danielr18 / connected-next-router

A Redux binding for Next.js Router compatible with Next.js.
MIT License
109 stars 30 forks source link

[Question] How to get URL parts in a Reducer? #67

Closed xaphod closed 3 years ago

xaphod commented 3 years ago

Hi @danielr18 thanks for making this.

I have a problem where i'm in a reducer, and the current path could be like /static1/static2/dynamic1/dynamic2, or it could be /dynamic1/dynamic2, and I need to extract & use dynamic1 and dynamic2. I want to find a solution that is agnostic to the values of staticN.

I would have used useRouter() and examined the query, but i'm in a reducer so I can't. I was hoping that using connected-next-router to put the router's state into redux would let me do this, but I cannot see how. Any ideas? (I've gotten to the point where I can see the router's state in redux)

danielr18 commented 3 years ago

@xaphod if you are in a reducer, you would only have access to the state of that reducer, not the whole store state.

It seems like you would have to extract dynamic1 and dynamic2 before it gets to the reducer. That way you can add them to the action data and access it in the reducer. There are multiple ways of doing this.

Let me know if that's possible

xaphod commented 3 years ago

Thanks for the quick reply. This project isn't using redux/actions to navigate, so i'm not sure where/when we would do that.

danielr18 commented 3 years ago

@xaphod It would have to be done where you dispatch the action. Alternatively, you could handle the LOCATION_CHANGE action the library uses in that reducer, and extract dynamic1 and dynamic2 from the payload's location object.

xaphod commented 3 years ago

OK thank you