久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      react組件mount好幾次怎么辦

      react組件mount好幾次的解決辦法:1、找到并打開“index.tsx”或者“index.js”文件;2、找到“root.render(<React.StrictMode><App /></React.StrictMode>);”代碼;3、將“React.StrictMode”高階組件包圍去掉即可。

      react組件mount好幾次怎么辦

      本教程操作環(huán)境:Windows10系統(tǒng)、react18.2.0版、Dell G3電腦。

      react組件mount好幾次怎么辦?

      React 18 componentDidMount重復(fù)執(zhí)行兩次的解決方案

      問題

      這兩天用create-react-app創(chuàng)建了一個(gè)新的React項(xiàng)目,在項(xiàng)目運(yùn)行的時(shí)候,似乎有意想不到的事情發(fā)生,組件的componentDidMount方法被觸發(fā)了兩次。

      react組件mount好幾次怎么辦

      而更早的項(xiàng)目,同樣采用create-react-app創(chuàng)建的一個(gè)項(xiàng)目,卻沒有這個(gè)問題,當(dāng)時(shí)真是一臉懵逼。。。

      排查

      首先想到的是前幾天將本地的create-react-app升級過,問題是不是create-react-app升級導(dǎo)致的,轉(zhuǎn)而一想應(yīng)該沒關(guān)系。后來去仔細(xì)比較了兩個(gè)項(xiàng)目的package.json文件,發(fā)現(xiàn)之前的項(xiàng)目,React用的是17.x版本;而問題項(xiàng)目用的卻是18.2.0版本的React。

      后來去React官方Github,果然找到關(guān)于18版本的一些Feature,鏈接:https://github.com/facebook/react/blob/main/CHANGELOG.md#breaking-changes:

      Stricter Strict Mode: In the future, React will provide a feature that lets components preserve state between unmounts. To prepare for it, React 18 introduces a new development-only check to Strict Mode. React will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount. If this breaks your app, consider removing Strict Mode until you can fix the components to be resilient to remounting with existing state.
      登錄后復(fù)制

      大意如下:

      在未來,React會(huì)提供一個(gè)新特性,該特性會(huì)使得組件取消加載后能維持狀態(tài)。React 18會(huì)再Strict Mode中引入一個(gè)新的開發(fā)模式。React將會(huì)對每一個(gè)組件自動(dòng)取消加載并重新加載。如果其干擾了你的應(yīng)用,移除Strict Mode就能夠修復(fù)組件重新加載的問題。(本人蹩腳的英語翻譯的,將就這看。。。)

      解決方案

      知道了原因之后,解決方案也很簡單,將index.tsx或者index.js文件里的React.StrictMode高階組件包圍去掉即可。

      修改前:

      root.render(   <React.StrictMode>     <App />   </React.StrictMode> );
      登錄后復(fù)制

      修改后:

      root.render(   // 去除React.StrictMode   // <React.StrictMode>     <App />   // </React.StrictMode> );
      登錄后復(fù)制

      至此,問題便完美的解決。

      推薦學(xué)習(xí):《react視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)