亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

路由后保留數組狀態掛鉤

路由后保留數組狀態掛鉤

肥皂起泡泡 2023-03-03 15:52:16
我試圖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路由到另一個頁面后如何保留鉤子?現在它正在重置(我已經調試了計算器值來檢查)。
查看完整描述

1 回答

?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

對于您的用例,我將創建一個新掛鉤,該掛鉤在掛載時從 localStorage 讀取并將更改持久保存到 localStorage。

useHooks 的useLocalStorage鉤子非常適合這里


查看完整回答
反對 回復 2023-03-03
  • 1 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號