Deepak β 06-23 Oct 2
Hey, I have reducer function that does some action onClick. However, i also want to navigate the page to some other url. After initial research i came to know that i can't useNavigate() inside a reducer function. How can i combine these two ?
16 Replies
@Deepak is this example from the react router docs helpful?
https://reactrouter.com/en/main/hooks/use-navigate
If I'm reading your question right, you're trying to call useNavigate inside of a function which violates the rules of hooks so it'll give you an error
Instead you need to call it at the beginning of your component and then use the result of it to navigate
yes, its helpful
Thanks
π you can use a β
reaction if you want to mark this thread as solved - happy it helped
https://reactjs.org/docs/hooks-rules.html this is a good guide for further reading incase you're curious
Rules of Hooks β React
A JavaScript library for building user interfaces
Could you tell me one more thing
i am trying to achive something like this
but it isn't able to read the searchinput when i click it lead me to
url/menu/undefined
how are you calling that function?
oops, i realised the mistake
handleCick();
looks like it doesn't have any arguments passed into it
haha yeah that'd do itSolution
thanks , got it
one more question. sorry for too many
what if i don't use navigate inside useEffect
is it necessary ?
i don't think use effect is needed, you can use it without it
just like this
yeah im pretty sure that'll work
yes it does work. But i show somewhere that navigate is desined to work with useEffect
although i am new so i not very sure
but anyway, thanks for you help. Sometime discussing with other make you see the problem π
talking it through is the best debugging technique there is
This thread hasnβt had any activity in 12 hours, so itβs now locked.
Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/902647189120118794/1026016606360444968