久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放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重復執(zhí)行兩次的解決方案

      問題

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

      react組件mount好幾次怎么辦

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

      排查

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

      后來去React官方Github,果然找到關于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.
      登錄后復制

      大意如下:

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

      解決方案

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

      修改前:

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

      修改后:

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

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

      推薦學習:《react視頻教程》

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