1

I am trying to keep some values using useContext API while moving with navigate("/toLocation") (let navigate = useNavigate()) but it refreshes and clears the value.

How do I use useNavigate and still keep the value in my context api

    const handleClick = () => {
        setSelectedNFT(nftData);
        navigate('/nft/detail')
    }
SMTP King
  • 429
  • 3
  • 12
  • Can you update your question to include all relevant code you've an issue working with? Can you clarify what is refreshing and what value is cleared? This may mean including this complete component code as well as the context code, and perhaps anything between if it's relevant. https://stackoverflow.com/help/minimal-reproducible-example – Drew Reese Feb 20 '22 at 06:57

2 Answers2

0

there was a link () that caused it to refresh when handleClick was clicked.

I simply used from react-router-dom instead and that fixed the issue

SMTP King
  • 429
  • 3
  • 12
0

As in the documentation of v6 react-router-dom https://reactrouter.com/docs/en/v6/hooks/use-navigate just provide the second optional argument 'replace'

 navigate('/nft/detail', { replace: true });

Hopefully, it will work fine. It worked in my case by the way. It stopped reloading just navigated me to the desired page and states were maintained in redux or context API.