我試圖useState在路由到另一個(gè)頁(yè)面后保留/保留反應(yīng)鉤子,現(xiàn)在假設(shè)我有一個(gè)名為數(shù)組的數(shù)組sections保存在useState鉤子中,在數(shù)組中我有 3 個(gè)項(xiàng)目廣告、推廣、建造當(dāng)我路由到另一個(gè)渲染時(shí),鉤子的值useState正在重置,其中包括數(shù)組。相關(guān)功能const [calculator, setCalculator] = useState({section: [], graphic: false}); //Sections array inside // Responsible to add item to the array, also promote and ads, just short version so you can understand function addPath(section){ if(section === "build"){ if(calculator.section.some(val => val === "build")){ let filteredArray = calculator.section.filter(item => item !== 'build') setCalculator({section: filteredArray}); $('.build').removeClass('active'); } else{ var joined = calculator.section.concat('build'); setCalculator({ section: joined }) $('.build').addClass('active'); } } }//Route from /start to /valuesconst Continue = () =>{ history.push("/values"); history.go(0);}// Check if the item exist in the array after routing the pagefunction checkArray(val) { return calculator.section.some(item => item === val);}建興興業(yè) <Route path="/start"> <p className="secondary"> ??? ????? ??????? (???? ????? ???? ????) </p> <Row className="margTop"> <Col lg="4"><img id="firstSelectors" className="build" onClick={() => addPath('build')} src={Code} alt="????? ?????" /></Col> <Col lg="4"><img id="firstSelectors" className="promote" onClick={() => addPath('promote')} src={Promotion} alt="????? ?????" /></Col> <Col lg="4"><img id="firstSelectors" className="ad" onClick={() => addPath('ad')} src={Advertise} alt="????? ??????" /></Col> </Row> <button onClick={Continue}>????</button> </Route> <Route path="/values"> {checkArray('ad') ? 'yes' : 'no'} </Route>useState路由到另一個(gè)頁(yè)面后如何保留鉤子?現(xiàn)在它正在重置(我已經(jīng)調(diào)試了計(jì)算器值來(lái)檢查)。
路由后保留數(shù)組狀態(tài)掛鉤
肥皂起泡泡
2023-03-03 15:52:16