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
Passing state option in navigate doesn't seem to work #96
Comments
I think the documentation is wrong. This means your code should look like this:
I replaced
with
Hope that helps |
Awesome, yes that was it. Many thanks! |
This needs to be opened as a defect because the docs are just plain wrong - still. I spent hours trying to work this out only to eventually find this post that fixed it in an instant. |
There are at least two places that should be improved/fixed: 1) https://codesandbox.io/s/l27754822mconst Invoice = props => (
<div>
{console.log(props.location.state)} {/*Only has key, nothing else*/}
<h2>Invoice {props.invoiceId}</h2>
</div>
); should be const Invoice = props => (
<div>
{console.log(props.location.state)} {/* Has `key` and `invoiceID` keys */}
<h2>Invoice {props.invoiceId}</h2>
</div>
); 2) https://reach.tech/router/api/navigateconst NewTodo = () => (
<TodoForm
onSubmit={async todo => {
let id = await createNewTodo(todo)
// put some state on the location
navigate("/todos", { newId: id })
}}
/>
)
const Todos = props => (
<div>
{todos.map(todo => (
<div
style={{
background:
// read the location state
todo.id === props.location.state.id
? "yellow"
: ""
}}
>
...
</div>
))}
</div>
) should be const NewTodo = () => (
<TodoForm
onSubmit={async todo => {
let id = await createNewTodo(todo)
// put some state on the location
navigate("/todos", { state: { newId: id }})
}}
/>
)
const Todos = props => (
<div>
{todos.map(todo => (
<div
style={{
background:
// read the location state
todo.id === props.location.state.newId
? "yellow"
: ""
}}
>
...
</div>
))}
</div>
) correct lines:
I agree with @akoolenbourke that this very issue should be still opened. |
The documentation was fixed via: #80 Problem is that the new documentation hasn't been deployed yet. We can keep this issue open until the documentation is updated. |
May I suggest docs fixes be deployed regularly. It's not like they need rigorous testing and it would really help. I would also like better docs in general. As I'm quite new to react et al I found that the docs just assumed I knew a lot more than I did, without really pointing me to where to learn more. I had a nightmare trying to figure out why props.location didn't exist in one of my components until I found and I had to put that somewhere up the hierarchy to get props.location. Was a pain to be honest because some components have props.location and some don't and I really don't know why it is this way. Note, I'm using Gatsby which uses @reach-router. |
Please update the docs. |
I had this issue today. The documentation is still incorrect. |
Can confirm the docs are still incorrect. https://reach.tech/router/api/navigate option section. |
please correct the wrong documentation, it costed me a day |
The documentation is still incorrect, please update https://reach.tech/router/api/navigate |
Can confirm that I also lost some time to this today, kindly help fix this for others. |
🎉docs have been updated to reflect the proper way to pass state into navigate. 🎉 |
I'm afraid the issue is still there: |
@mtliendo @ryanflorence This issue has been fixed over a year ago in e8325ae - but still confuses people. May I ask why? |
@mtliendo I'm afraid I ran into this issue today, luckily I stumbled on this link pretty quickly but the documentation is still showing the wrong syntax. |
@ryanflorence I checked the document's source to see if I can contribute. It is already fixed in the source markdown. It just has not been published yet for some reason. |
I think we should open another issue. docs are still wrong |
Can you please re-open. The docs are still incorrect. Similar to @RifRaf44, I'm glad I found this issue. |
@blainekasten , as the most active in this repo since the beginning of this year, can you take a look at this issue? |
I'm pinging Ryan for this. I don't have website admin. I'll try to get this fixed |
The docs have been updated! Sorry for the delay everone! |
Hi! |
@odalysadam How do you "read" the state? An example code snippet would be helpful to answer. |
@jacekk Of course, sorry. Currently just logging |
@odalysadam I dunno why the whole app might gets realoded when using Just guessing though, as I've never had a problem with missing |
@jacekk Thanks, I'll try an absolute path and see if the full reload still happens. |
@jacekk Using an absolute path changed nothing, but state is most likely null because on reload everything gets lost. Is triggering a full reload something that navigate even does or should I be looking elsewhere for the problem? |
@odalysadam It works for me: Can you replicate your case? Or share your project/code? Simple |
@jacekk Ok, then the problem lies elsewhere. Thank you very much! |
@HenryKenya Try this: navigate("/auth", { state: { fooKey: "fooValue" }}); |
@jacekk done that. Still having the same issue of seeing only keys
|
@HenryKenya Then try to replicate in some other component or in a separate project. This definitely works: |
@jacekk checked out the sandbox. It has the same issue of just showing keys and no value |
@jacekk checked it out again. The issue must be somewhere else. Let me try debug it |
could the issue be that I am trying to access state using the |
@HenryKenya |
The sandbox works..confirmed 😂 |
This line --> https://github.com/reach/router/blob/master/src/index.js#L69 gets @HenryKenya |
When
state
option is provided tonavigate
, it is not available in theprops.location.state
object for the matched components.Example sandbox: https://codesandbox.io/s/l27754822m
Steps to reproduce:
props.location.state
forInvoice
andInvoices
components.I have taken the example from documentation and updated the
Invoices
component to havestate
option { invoiceID: provided_id } for thenavigate
function.props.location.state
seems to only havekey
and no other information for the matchedInvoice
andInvoices
components.Is it a bug or am I not using the option correctly? 😕
The text was updated successfully, but these errors were encountered: