-
Notifications
You must be signed in to change notification settings - Fork 27k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible to pass state to next/router? #771
Comments
Currently that's not possible and I don't think we'll support it immediate future. |
But let's open this and see whether we really really need this. |
I think you should do this by passing query parameters. |
I mean you can achieve a similar thing by passing in query parameters like nkzawa said, but I'd rather not pollute my the url with |
You can use |
Well I suppose so, but if I were to use I'm porting over a project to next.js and haven't run into any major compatibility problems since I found ways to refactor codes to make it work, it's just inconvenience and verbosity at this point that can be improved and all this issue is. Thanks all for your input. |
Closing this cause of inactivity 👍 |
A little over a year since I brought this up, but is this still not supportable? |
You can save any required state in localStorage, sessionStorage, a variable, a Redux store, etc. and then use it in getInitialProps (checking you are running client side). |
i think nextjs should start supporting react-router |
I want same functionality. Earlier I was using CRA and there I used to send state to routed component. I wants same functionality with nextjs. What @sergiodxa suggested in workaround but library support will be good. |
Same here, would be great if it will be possible pass data with |
I want same functionality. Any update on this ? |
me too |
Another question related to this issue is:
|
I have a scenario where I have a clickable component. clicking it would open its details page. With current next.js i can pass the projects id and then get the details using useEffect and axios call but since i already have the data i really wished i could send the state to that page. |
Would be nice to have this. @sergiodxa Yes, there are many ways to pass temporary messages but the advantage of having it tied to the router is that we don't have to write extra code to manage the life cycle of the values we pass around. They will be tied to router navigation events and unavailable in other contexts. For example, If I push it to my redux store, then I have to exclude it from being persisted to local storage and also dispatch a clear message action on the receiving end. If I add it the query string it becomes a perma link that uses can bookmark and later return to. But it I can push it to the next page and only the next page via Very similar to connect-flash |
No solution since Jan 15 2017 ?
|
Would be nice to have this in nextjs |
if you want to hide query params, just use like this
than the main page, you can see only
|
oh yeah, that's true. Btw I find it really awkward that router requires two strings. |
For those who want to make it, you can use Next.js provides the |
Willing to reconsider this, however it needs a great proposal on how to prevent users from shooting themselves in the foot using history state given you can't access history state server-side for example, it would be very easy to cause hydration issues. Do note that re-opening this does not mean that I'm not going to write a RFC for this feature, that would be up to the people that want to have this feature. |
|
This comment has been minimized.
This comment has been minimized.
This is still not internal state, it's a query string with a mask feature, which is quite an odd thing to do in my opinion. |
I'm not super familiar with React ecosystem. But it appears that you can do
When you go back and check before PopState, the option prop will contain We only need this in the |
It seams like another temporal hack, not intentional behavior. Expected options are listed in the docs. But something similar to this would be great to have as expected behavior |
@likerRr Apparently all of the options parameter is being passed to |
Seems like it is. Anyway, until it's documented, it can't be used in prod, because implementation of passing options can be changed anytime 😞 Another problem (?) with history state, that it's persistent. And if you play with "back"/"forth" buttons you will mostly get unexpected behavior. As far as I understand the use case of what community wants is to pass data which will leave only during transition from one page to another and we don't have to bother of clearing it. And this data should not appear again when user clicks "back/forth" in a browser. Otherwise this is the best proposition (imo). |
Would be nice if next js considered this |
This worked for me, you can use 1: in the Subscription page: you can see value of |
I understand the issue and the convenience of being able to pass along state in router.push. Would the following be a temporary solution? Use the browser's history.pushState, as well as a local useState of a location string, and when we want to redirect, we use history.pushState as well as setting a new value to local location state. An update to the location-string-state should trigger a useEffect run. Then have a JS swich statment which evaluates the location and returns components based on the location. And if we wanna get to the same component without history.pushState (for exp. if the user refreshes the page), we can make the location a dynamic next query param. -- did not try it, just trying to think of workarounds. |
What if in my in my The only solution is to send along a |
Hello, are there any explanations from the vercel / next team, why it would be problematic to pass a state to the router/link ? It looks like this topic is required by a lot of people (even from me - and i don't see any valid reason against this in this chat). My usecase is tostore the scroll position (not of the page, the scroll position from an overflow:hidden div), so that a user can go back and have the same position inside this div as before. I could imaging to have a state property for the NextLink.
This param can be passed to the router push. The next framework is using already the state for their purpose. I and mostly all in this thread would appreciate if request can be added to the code. Thanks a lot for all your nice work and the nextjs framework |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
As said earlier: #771 (comment) |
Okay, I agree, this is a valid concern. |
Actually, you can do it with hidden query params. In nextjs you can redact query params with the |
@meilechwieder : Thanks for this idea. It's not a clean solution but with a dedicated React Link Component (only for this scrolling case) it could work out without creating so much dept. |
This comment has been minimized.
This comment has been minimized.
I am still waiting for this functionality, hope it gets implemented in near future. |
Yeah this feature is really needed. The hidden query params work around is nice in the mean time, but we may need to pass complex objects to the next page. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
With react-router I was able to do:
browserHistory.push({ pathname: "/", state: { message: 'This is a message forwarded in a state.' } })
Is it possible to do something like this using next/router or a way to redirect the user and also pass some value over to the new page?
The text was updated successfully, but these errors were encountered: