admin管理员组文章数量:1430716
What i want is when i click on button the React ponent
should re-render
And i also Don't want to re-Render the plete webiste on this particular ponent has to re-render
It tried using hooks to re-render the page but it gives me error as shown above and also tried using
forceUpdate()
Hook but that still gives me the same error
re-Render the ponent
This is the javascript of same file
const LeftCard = ({ cardData: cardComponent, getFileName }) => {
let emptyArr=[];
let pageNo=10;
const [loadComponentByPage,setPageArr]=useState(undefined);
if(pageNo){setPageArr(emptyArr);console.log(emptyArr)}
pageNo<10?pageNo=10:''
pageNo===10 ? pageChange(0):null
function pageChange(num){ <-------- This function is getting called on button click
if(cardComponent !==undefined) {
const mainArray=cardComponent.sort((a,b)=>b.id-a.id).map(val=>{return val});
const arrayLength=cardComponent.length;
if(pageNo===10 && num===-10)return;
if(arrayLength<pageNo && num ===10)return;
pageNo+=num
emptyArr=[]
for(let i=pageNo-10;i<=pageNo;i++){
if(mainArray[i]!==undefined){
emptyArr.push( mainArray[i])
}
}
}
}
if(loadComponentByPage ===undefined )return
This is jsx of same file
return (
<div>
{loadComponentByPage.sort((a, b) => (a.id - b.id))
.map((element, i) => {
return (
<div key={i}>
<span className="image-overflow flex ">
<img className="left-sec-image ml2 link Dim" src={element.image} alt={element.topicname}></img>
<div>
<h4 className=" ml4 mr2 light-purple">{element.topicname}</h4>
</div>
</span>
</div>
);
}).reverse()}
<div>
<div className='ml5'>
These are the button that calling the function-------> <button className='ml7' onClick={()=>{pageChange(-10);doNothing()}}><ArrowBackIosSharpIcon /></button>
These are the button that calling the function-------> <button className='ml6' onClick={()=>{pageChange(10);doNothing()}}><ArrowForwardIosSharpIcon /></button>
</div>
</div>
</div>
)};
export default LeftCard;
What i want is when i click on button the React ponent
should re-render
And i also Don't want to re-Render the plete webiste on this particular ponent has to re-render
It tried using hooks to re-render the page but it gives me error as shown above and also tried using
forceUpdate()
Hook but that still gives me the same error
re-Render the ponent
This is the javascript of same file
const LeftCard = ({ cardData: cardComponent, getFileName }) => {
let emptyArr=[];
let pageNo=10;
const [loadComponentByPage,setPageArr]=useState(undefined);
if(pageNo){setPageArr(emptyArr);console.log(emptyArr)}
pageNo<10?pageNo=10:''
pageNo===10 ? pageChange(0):null
function pageChange(num){ <-------- This function is getting called on button click
if(cardComponent !==undefined) {
const mainArray=cardComponent.sort((a,b)=>b.id-a.id).map(val=>{return val});
const arrayLength=cardComponent.length;
if(pageNo===10 && num===-10)return;
if(arrayLength<pageNo && num ===10)return;
pageNo+=num
emptyArr=[]
for(let i=pageNo-10;i<=pageNo;i++){
if(mainArray[i]!==undefined){
emptyArr.push( mainArray[i])
}
}
}
}
if(loadComponentByPage ===undefined )return
This is jsx of same file
return (
<div>
{loadComponentByPage.sort((a, b) => (a.id - b.id))
.map((element, i) => {
return (
<div key={i}>
<span className="image-overflow flex ">
<img className="left-sec-image ml2 link Dim" src={element.image} alt={element.topicname}></img>
<div>
<h4 className=" ml4 mr2 light-purple">{element.topicname}</h4>
</div>
</span>
</div>
);
}).reverse()}
<div>
<div className='ml5'>
These are the button that calling the function-------> <button className='ml7' onClick={()=>{pageChange(-10);doNothing()}}><ArrowBackIosSharpIcon /></button>
These are the button that calling the function-------> <button className='ml6' onClick={()=>{pageChange(10);doNothing()}}><ArrowForwardIosSharpIcon /></button>
</div>
</div>
</div>
)};
export default LeftCard;
Share
Improve this question
asked Nov 30, 2022 at 12:18
SHERAWAT LOKESHSHERAWAT LOKESH
271 silver badge8 bronze badges
1 Answer
Reset to default 5The problem you are having is that you've created an infinite loop of renders.
It's a mon pitfall in React and is something to look out for.
In React, a ponent re-renders whenever one of these 2 cases occurs:
- Whenever there is a change in the ponent's local state.
- Whenever any of it's parent ponents themselves are re-rendered.
So what happens is the following:
- When the ponent renders, the value of
pageNo
is 10, which is "truthy". - This means that we enter the
if
block andsetPageArr
is called, thus setting the ponent's state. - The ponent is re-rendered due to the state update.
- The value of
pageNo
is still 10, so we enter theif
block and set the state again.
And this cycle repeats, creating an infinite loop.
Typically, when faced with an infinite loop, the browser would freeze and ultimately crash.
The error you are getting is due to React "catching" the problem and preventing the freeze.
To fix it we have 2 options:
- Since in this case we already know the initial value of the state is an empty array, we can simply initialize it as such.
Example:
const [loadComponentByPage,setPageArr]=useState([]);
- If we don't know the initial value, such as the case with values fetched from an external API / etc.. We can utilize the
useEffect
hook.
Example:
useEffect(() => {
setPageArr(emptyArr)
}, []) // A `useEffect` hook with an empty dependency array should run only once when the ponent mounts.
Either way, this bit needs to go -> if(pageNo){setPageArr(emptyArr);console.log(emptyArr)}
本文标签:
版权声明:本文标题:javascript - Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. (React-js) - S 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745560043a2663420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论