简体   繁体   中英

Too many re-renders. React limits the number of renders to prevent an infinite loop.?

I'm getting an error while rendering the following component:

Too many re-renders. React limits the number of renders to prevent an infinite loop.?

import React, { useState } from 'react';
     
function My() {
  const [NAME, setValue_] = useState([{name:"",lname:""}]);  //      usestate
  let arr = [{ name: "man", lname: "Kumar" }];    //.......creating array for passing to set the value for State..
      
  setValue_(arr); //............passing array to Update data......

  console.log(NAME);   //..........Want to see updated Data....
        
  return(<> </>) //............... i didn't return anything........
}

 import React, { useState } from 'react'; function My() { const [name, setName] = useState([{ name: "man", lname: "Kumar" }]); return(<>helloworld</>) }

That's it. Just pass the array directly to the state instead of updating it later.If you really need to play it this way, then use a useEffect. Also, please keep a naming consistency. setValue doesn't relate well to name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM