You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[QUESTION]How to use reach router with React Context API? #328
Also when I console.log(this.props) , it returns the same result as this.context
Can someone help me understand why this is happening? How to refactor with context properly?
changed the title [-]How to use reach router with React Context API?[/-][+][QUESTION]How to use reach router with React Context API?[/+]on Nov 29, 2019
@mtliendo thanks for your reply! I wrapped the entire app under the GlobalContext.consumer, but I still want to pass the url params id as a prop just like this example https://reach.tech/router/example/url-params.
Now for Component A, I can't access id from this.props, and when I try to console.log(this.props), it logs the same result as this.context. Sorry, I don't have a public repo for now.
A bit tough to know exactly what you're looking for, but I hope this helps 😃 In this example, the id can still be access on props, while a lower component can still access context set on the provider:
Activity
[-]How to use reach router with React Context API?[/-][+][QUESTION]How to use reach router with React Context API?[/+]chloesun commentedon Jan 13, 2020
@ryanflorence Could you please take a look at my issue, I've stuck with this one for a while. Thank you very much!
mtliendo commentedon Jan 14, 2020
Hey @chloesun sorry for the delay.
I get what you're trying to do, though I'm not seeing how you're wanting to use
contextto pass in theid.Is there a repo or codesandbox I could look at? I'd be happy to get you on the right track 😄
chloesun commentedon Jan 14, 2020
@mtliendo thanks for your reply! I wrapped the entire app under the
GlobalContext.consumer, but I still want to pass the url paramsidas a prop just like this example https://reach.tech/router/example/url-params.Now for Component A, I can't access
idfromthis.props, and when I try toconsole.log(this.props), it logs the same result asthis.context. Sorry, I don't have a public repo for now.mtliendo commentedon Jan 14, 2020
A bit tough to know exactly what you're looking for, but I hope this helps 😃 In this example, the
idcan still be access on props, while a lower component can still access context set on the provider:https://codesandbox.io/s/reach-router-starter-with-context-6t5tq?fontsize=14&hidenavigation=1&theme=dark
mtliendo commentedon Jan 14, 2020
Hope that helps out, I'll go ahead and close, though feel free to post if there's something else going on.
chloesun commentedon Jan 14, 2020
Thanks @mtliendo , my Component that uses url params as prop is deeply nested in the app, I will try to refactor my code.
chloesun commentedon Jan 16, 2020
I finally figure out this issue:
ComponentAis wrapped with HOC, and by adding{...this.props}to ComposedComponent,ComponentAcan access the url params fromthis.propsPlease refer this issue https://stackoverflow.com/questions/45873414/passing-react-context-through-an-hoc-to-a-wrapped-component