Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Huzaifa Rasheed
Huzaifa Rasheed

Posted on

     

How to Avoid Default Props Render Trap in React

What is it?

Let's say we have a component with a default prop like this

importReact,{useEffect,useState}from"react";constRerenderChild=({value=[]})=>{const[valueFromProp,setValueFromProp]=useState([]);useEffect(()=>{setValueFromProp(value);},[value]);return(<div>{/* ...component */}</div>);};exportdefaultRerenderChild;
Enter fullscreen modeExit fullscreen mode

Whenever thevalue prop is nullish (not set by the calling component), theuseEffect causes the component to render infinitely. This is theDefault Props Render Trap. We get something like this in the browser console.
Infinite Re-rendering in React Component

Why It Happens

When we don't provide the value for thevalue prop it takes the default value provided as argument, which in our case is[]. This triggers theuseEffect hook which updates thevalueFromProp state. The state change causes the component to re-render.

Now when the component re-renders, it takes the new prop values which again are the default one's. This again triggers theuseEffect and the whole cycle repeats. That's why we end up with an infinite loop.

The Solution

We have to make the default prop values a part of our component definition. We can do that in these ways.

1. Use defaultProps property.

We can set the default props value by using the component'sdefaultProps property. Our component now becomes

importReact,{useEffect,useState}from"react";constRerenderChild=({value})=>{const[valueFromProp,setValueFromProp]=useState([]);useEffect(()=>{setValueFromProp(value);},[value]);return(<div>{/* ...component */}</div>);};RerenderChild.defaultProps={value:[],};exportdefaultRerenderChild;
Enter fullscreen modeExit fullscreen mode

2. Declare default props as constants.

We can declare constants outside of our component and set them as our default prop value.

importReact,{useEffect,useState}from"react";constDEFAULT_VALUE=[];constRerenderChild=({value=DEFAULT_VALUE})=>{const[valueFromProp,setValueFromProp]=useState([]);useEffect(()=>{setValueFromProp(value);},[value]);return(<div>{/* ...component */}</div>);};exportdefaultRerenderChild;
Enter fullscreen modeExit fullscreen mode

Hope this helps you to avoid the infinite loop. Thanks 😊.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
amerllica profile image
Amer Lotfi Orimi
I'm a developer
  • Location
    Tehran, Tehran Province, Iran
  • Joined

A pretty copy of this awesome post on Stack Overflow:stackoverflow.com/q/71060000/6877799

CollapseExpand
 
kenanyildiz profile image
Kenan Yıldız
  • Joined

What if I wanted to pass a default empty object or empty array to the multiple props. Would you create just 2 and use for each prop? or would you create brand new default variables (even its has the same value) for each props?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A software enthusiast ✨, currently living in the javascript universe. 🌌
  • Location
    Islamabad, Pakistan
  • Work
    Software Engineer
  • Joined

More fromHuzaifa Rasheed

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp