reactjs - componentDidUpdate causing infiinite loop -
i have been working on small todo application learn , head around react. has been going according plan until started completed section of application. problem trying solve pretty simple, however, cannot seem wrap head around going on. trying invoke createcompletedlist method can create of items have been completed. although, when method invoked in componentdidupdate method creates infinite loop crashes browser. there way can call createcompletedlist method safe kind of behaviour?
any sort of advice or information appreciated.
import react, { component } "react"; class completed extends component { constructor(props) { super(props); console.log(props); this.state = { completeditems: [] }; this.checkindex = this.checkindex.bind(this); this.createcompletedlist = this.createcompletedlist.bind(this); } checkindex(event) { console.log("index value " + this.props.indexvalue); } createcompletedlist() { const completedindex = this.props.indexvalue; const completedarray = this.state.completeditems; if (completedindex) { completedarray.push(this.props.arrayitems[completedindex]); this.setstate(prevstate => { return { completeditems: completedarray }; }); } console.log(this.state.completeditems); } componentdidupdate() { this.createcompletedlist(); } render() { let completedarray = this.state.completeditems; const retrievelist = completedarray.map((elm, index, arr) => { return ( <div classname="check-list-row checked" key={index}> {elm} </div> ); }); return ( <div classname={this.props.istaskchecked ? "completed-container" : "hide"}> <div classname="completed-row"> <h1>completed</h1> </div> <div> {retrievelist} </div> </div> ); } } export default completed;
calling this.setstate() cause update. can use shouldcomponentupdate(nextprops, nextstate) determine whether or not update. default method returns true. compare nextstate.completeditems this.state.completeditems , return false if don't want update.
Comments
Post a Comment