Skip to content
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

Raise route params higher in the component tree #2042

Closed
KrisCoulson opened this issue Mar 19, 2021 · 5 comments · Fixed by #2043
Closed

Raise route params higher in the component tree #2042

KrisCoulson opened this issue Mar 19, 2021 · 5 comments · Fixed by #2043

Comments

@KrisCoulson
Copy link
Contributor

KrisCoulson commented Mar 19, 2021

Currently, the params from useParams are only accessible inside of a route component. With the introduction of the Set component, we still cannot have dynamic Layouts. My initial suggestion is to raise the ParamsContext.Provider higher in the component architecture to be able to access the params in the Set component. Currently, the ParamsContext.Provider is initialized in the PageLoader component

<ParamsContext.Provider value={this.state.params}>
<PageLoadingContext.Provider value={{ loading: false }}>
<PageFromLoader {...this.state.params} />
</PageLoadingContext.Provider>
</ParamsContext.Provider>

Another solution would be to make params available as a prop in the Set component that passes those params to our Layout by default. This way you don't have to know to useParams you just have access to them in your Layout.

@thedavidprice
Copy link
Contributor

Looping in @Tobbe

@KrisCoulson did you happen to see this PR that just merged today? It adds some nice, new features to Routes.js:
#1898

See this syntax reference from the original RFC discussion:
https://community.redwoodjs.com/t/redwood-router-with-layouts-context-providers-etc/1342/25

Possible this will provide the capability you're looking for?

@KrisCoulson
Copy link
Contributor Author

@thedavidprice I have been talking with @Tobbe about this. Told him I would create the issue. I pulled the canary version to test the upgrade for my use case. The Set component makes it possible to do what I am trying to do now. But it's a bit of a pain because I would have to manually parse the location.pathname to get the id to make a request

Something like this

  const { pathname } = useLocation()
  const eventId = pathname.split('/').filter((item) => !!item.length)[1]
  const { loading, error, data } = useQuery(QUERY, {
    variables: { eventId: parseInt(eventId, 10) },
  })

@thedavidprice
Copy link
Contributor

Thanks! I just saw Tobbe opened #2043 simultaneously and assumed you're collaborating.

@thedavidprice
Copy link
Contributor

@Tobbe could you provide a final example like the above in the PR description?

@Tobbe
Copy link
Member

Tobbe commented Mar 20, 2021

You bet!

Tobbe added a commit that referenced this issue Mar 20, 2021
* Make useParams usable in layouts

Fixes #2042
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants