亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

透過自己點(diǎn)擊連結(jié)來重新渲染React Router元件。
P粉295728625
P粉295728625 2023-07-27 22:31:16
0
1
616
<p>我用 <code>react-router-dom</code> v6</p> <h1>代碼</h1> <pre class="brush:php;toolbar:false;"><NavLink to="/pathOne" className="ripple">label1</NavLink> <NavLink to="/pathTwo" className="ripple">label2</NavLink></pre> <h1>問題</h1> <p>當(dāng)你點(diǎn)擊一個連結(jié)或另一個連結(jié)時,Route元件會如預(yù)期進(jìn)行渲染。但是,如果"/pathOne"處於活動狀態(tài)並且我再次點(diǎn)擊它,則什麼都不會發(fā)生。 </p><p>是否有一種方法可以透過點(diǎn)擊活動連結(jié)來強(qiáng)制重新渲染路由元素? </p><p>如果設(shè)定了reloadDocument屬性,我可以刷新整個頁面,但這不是一個可行的選項。 </p><p><code></code></p>
P粉295728625
P粉295728625

全部回覆(1)
P粉432906880

If all you really want is for the route component to rerender each time the link to its route is clicked then just have those components call the useLocation hook. Each time the link is clicked a new location object reference is created. The new location object reference is enough to trigger the component using it to be rerendered.

Example:

const PathOne = () => {
  useLocation();

  useEffect(() => {
    console.log("PathOne rerender");
  });

  return <h1>PathOne</h1>;
};

const PathTwo = () => {
  useEffect(() => {
    console.log("PathTwo rerender");
  });

  return <h1>PathTwo</h1>;
};
function App() {
  return (
    <div className="App">
      <NavLink to="/pathOne" className="ripple">
        label1
      </NavLink>
      <NavLink to="/pathTwo" className="ripple">
        label2
      </NavLink>
      <Routes>
        <Route path="/pathOne" element={<PathOne />} />
        <Route path="/pathTwo" element={<PathTwo />} />
      </Routes>
    </div>
  );
}

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板