我試圖useState在路由到另一個頁面后保留/保留反應鉤子,現在假設我有一個名為數組的數組sections保存在useState鉤子中,在數組中我有 3 個項目廣告、推廣、建造當我路由到另一個渲染時,鉤子的值useState正在重置,其中包括數組。相關功能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);}建興興業 <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路由到另一個頁面后如何保留鉤子?現在它正在重置(我已經調試了計算器值來檢查)。
路由后保留數組狀態掛鉤
肥皂起泡泡
2023-03-03 15:52:16