I'm trying to replace a Backbone.Marionette App to React and am facing difficulty thinking about query params. I think I'm missing a really simple peace in understanding this pattern so I apologize if this question is totally nonsense. I would appreciate any support or just pointing me to some direction that I can google more specifically.
There's a /users
page which lists users and you can filter the users via search bar. So if you want to filter the users which contain 'joe' in their username, I would make a request to the server with query params like /users?username=joe
. In addition I am able to paginate by adding a page
parameter, too (/users?username=joe&page=1
).
If I only think about the functionality, the flow would probably be
- The Client inserts
joe
to the input element and clicks Search. - Clicking the Search button fires an
Action
(like Action.getUser). - The
Action
makes a request to the server and receives the results - The
Dispatcher
dispatches a function with the results payload to whomever (usually theStore
) is interested in theAction
. - The
Store
's state changes with the new result received by theAction
- The View (
Component
) re-renders by listening to theStore
's change.
and it works as expected. However, I would like the Client to be able to bookmark the current filtered result and be able to come back to the same page some time later. This means I will need somewhere to save explicit information about the search term the Client made, which is usually the url (am I right?). So I will need to update the url with query parameters to save the search term (/users?username=joe&page=1
).
What I'm confused is where and when to update the url? What I can come up with right now are the 2 options below - and they don't seem to be clean at all.
Option 1
- The Client inserts
joe
to the input element and clicks Search. - Clicking the Search button fires a transition of the ReactRouter with the new query params (
/users?username=joe&page=1
). - The View (
Component
) receives the new params viathis.props.params
andthis.props.query
. - The View (
Component
) fires anAction
likeAction.getUser
depending on the query params it receives - in this caseusername=joe&page=1
.
after this, it is the same as above
Option 2 (only 6 is different from what I explained above)
- The Client inserts
joe
to the input element and clicks Search. - Clicking the Search button fires an
Action
(like Action.getUser). - The
Action
makes a request to the server and receives the results - The
Dispatcher
dispatches a function with the results payload to whomever (usually theStore
) is interested in theAction
. - The
Store
's state changes with the new result received by theAction
- The View (
Component
) re-renders by listening to theStore
's change. And somehow (I don't know how, yet) updates its url depending on itsprops
(likethis.props.searchusername
, andthis.props.searchpage
)
What is the best practice on handling query params? (or this may not be specific to query params) Am I completely misunderstanding the design pattern or architecture? Thanks in advance for any support.
Some articles I've read