-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
improve support for passing props down to children from a layout... #3412
Comments
I've just been trying to do this today and I've been finding that the props in my page components aren't being updated to match those being sent from the layout when they change. They seem to remain on the value of the first render. I'm using I pass them down like so:
Gatsby's |
My solution was to create a class component and pass the props to child component using childContextTypes static object. Define below context in parent Layout component.
Then populate the value using getChildContext() in Layout class
Now you can get the value in child component by defining context types like below
Now you can access the property value in child component using the context like below
|
@jeblister are you sure that's doing what you think? iirc, context-driven updates end up getting blocked by
in short, i think what you are doing there is basically the same as |
@busticated this solution work for me. I tried the solution of @baseten without result. |
@jeblister i put together a demo showing the issue: https://gist.github.com/busticated/1170897ccabeb566951a209d673b9eb1 TLDR; |
I'm going to put in a fix for the Re: the original suggestion of passing along all props by default, a bit wary of that -- what if someone intentionally removed a couple of keys from the Route props dict, then passed that through to |
@calcsam I'm not too familiar with the architecture so forgive me if this won't work, but how about allowing the following:
Then internally the children function merges this object into an explicit property say Object equality could be an issue here, but not sure if that's the developer's or gatsby's responsibility to take care of |
I'm trying to pass down the props like
I think it's because it's passing down My use case is, I'm using Contentful to grab some 'pages' (my content type) with various fields like content, title, image, etc. I don't want to have a custom query for each page, I'd rather just have a query in the layout and pass down the data through props into each page that is loaded. |
from a discussion over in discord...
say you want to pass some props down to a child component from within a layout (e.g.
layouts/index.js
):digging into the code, i think the issue is here - specifically:
so when we pass down props
{ foo: true }
from the layout,props.history
is now undefined.short-term, i'm wondering if there's any downside to always forwarding the original props along with custom additions (e.g.
props.children({ ...props, foo: true })
)?longer-term, maybe it makes sense to merge incoming props or more formally support passing custom props down to
props.children()
?Environment
Gatsby version: 1.9.149
Node.js version: 8.9.3
Operating System: macOS 10.12.6
The text was updated successfully, but these errors were encountered: