"too many re-renders. react limits the number of renders to prevent an infinite loop." Code Answer's
You're definitely familiar with the best coding language TypeScript that developers use to develop their projects and they get all their queries like "too many re-renders. react limits the number of renders to prevent an infinite loop." answered properly. Developers are finding an appropriate answer about too many re-renders. react limits the number of renders to prevent an infinite loop. related to the TypeScript coding language. By visiting this online portal developers get answers concerning TypeScript codes question like too many re-renders. react limits the number of renders to prevent an infinite loop.. Enter your desired code related query in the search bar and get every piece of information about TypeScript code related question on too many re-renders. react limits the number of renders to prevent an infinite loop..
too many re-renders. react limits the number of renders to prevent an infinite loop.
I suspect that the problem lies in the fact that you are calling your state setter immediately inside the function component body, which forces React to re-invoke your function again, with the same props, which ends up calling the state setter again, which triggers React to call your function again.... and so on.
const SingInContainer = ({ message, variant}) => {
const [open, setSnackBarState] = useState(false);
const handleClose = (reason) => {
if (reason === 'clickaway') {
return;
}
setSnackBarState(false)
};
if (variant) {
setSnackBarState(true); // HERE BE DRAGONS
}
return (
<div>
<SnackBar
open={open}
handleClose={handleClose}
variant={variant}
message={message}
/>
<SignInForm/>
</div>
)
}
Instead, I recommend you just conditionally set the default value for the state property using a ternary, so you end up with:
const SingInContainer = ({ message, variant}) => {
const [open, setSnackBarState] = useState(variant ? true : false);
// or useState(!!variant);
// or useState(Boolean(variant));
const handleClose = (reason) => {
if (reason === 'clickaway') {
return;
}
setSnackBarState(false)
};
return (
<div>
<SnackBar
open={open}
handleClose={handleClose}
variant={variant}
message={message}
/>
<SignInForm/>
</div>
)
}
Comprehensive Demo
See this CodeSandbox.io demo for a comprehensive demo of it working, plus the broken component you had, and you can toggle between the two.
All those coders who are working on the TypeScript based application and are stuck on too many re-renders. react limits the number of renders to prevent an infinite loop. can get a collection of related answers to their query. Programmers need to enter their query on too many re-renders. react limits the number of renders to prevent an infinite loop. related to TypeScript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about too many re-renders. react limits the number of renders to prevent an infinite loop. for the programmers working on TypeScript code while coding their module. Coders are also allowed to rectify already present answers of too many re-renders. react limits the number of renders to prevent an infinite loop. while working on the TypeScript language code. Developers can add up suggestions if they deem fit any other answer relating to "too many re-renders. react limits the number of renders to prevent an infinite loop.". Visit this developer's friendly online web community, CodeProZone, and get your queries like too many re-renders. react limits the number of renders to prevent an infinite loop. resolved professionally and stay updated to the latest TypeScript updates.