Re-render the React Router component by clicking the link yourself.
P粉295728625
P粉295728625 2023-07-27 22:31:16
0
1
433

I use react-router-dom v6

code

label1 label2

Question

When you click one link or another, the Route component renders as expected. However, if "/pathOne" is active and I click on it again, nothing happens.

Is there a way to force a route element to be re-rendered by clicking on an active link?

I could refresh the entire page if the reloadDocument attribute was set, but that's not a viable option.

P粉295728625
P粉295728625

reply all (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 theuseLocationhook. Each time the link is clicked a newlocationobject reference is created. The newlocationobject reference is enough to trigger the component using it to be rerendered.

Example:

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

PathOne

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

PathTwo

; };
function App() { return ( 
label1 label2 } /> } />
); }

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!