-
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
Comparing a component's "type" to the component is not producing expected result #3486
Comments
If I add some console logs like this: const Foo = (props) => {
const newChildren = React.Children.map(props.children, child => {
console.log(child.type)
console.log(Bar)
return child.type === Bar
? React.cloneElement(child, { hello: 'world' })
: <div>not same</div>
})
return <div>{newChildren}</div>
} Notice the output suggests they are the same "component", but again the truthiness is false leading to the "not same" output |
Ah, think I figured it out
We're cloning the children prop. |
We should probably just pass it in directly then yeah? |
@KyleAMathews I don't fully understand the context of the code you showed, but do you mean something like this to preserve children?:
|
Yeah exactly. When you have a chance, could you test that to make sure it works and then PR the change? |
@KyleAMathews I followed all the steps to use
...and I didn't get a log in dev tools. Also made the props change because I'm not sure if this is a SSR method, but that didn't work. I'm sure I'm doing something wrong here with dev work in Gatsby, but this is a needle in a haystack situation for me since I'm pretty unfamiliar with Gatsby setup. Any pointers? |
Yeah, making changes to |
@KyleAMathews we're making progress, made a production build then
... to include the |
Awesome! That's surprising the same change didn't work in dev. Double check your change is being applied? The children component must be being cloned elsewhere still. |
I probably didnt say the last message clear enough, the only thing I did in production was the console log. So production always works without any changes but development doesn't work |
Oh ok, gotcha. So mystery is still unsolved |
I just haven't had time to keep looking at this yet, but I will soon I hope |
FWIW, we can confirm @bradwestfall 's observation that this behavior currently only affects the local |
I'm really sorry I've been absent from this thread, I still really want to get this solved but I got pulled away on other projects. But bottom line is that I can't use Gatsby at the moment with some important components I have because of this. I hope to look at it again soon but I'm very unfamiliar with Gatsby internals so it's hard to know where to start if this helps, I get the same problem with NextJS. So there must be some common trait with these SSR tools, if that gives you any clues. I've never done anything SSR related except for using Next and Gatsby, and also never had this problem before with components that compare types |
I'm also not able to spend the time on this that I wanted to unfortunately. For anyone who's interested, I set up @bradwestfall 's test case in an example repo here: https://github.com/fabrictech/gatsby/tree/example-check-types/examples/check-types |
Ahh looks like react-hot-loader gaearon/react-hot-loader#304 |
@KyleAMathews What are your thoughts on auto-injecting that babel-plugin to fix this in the develop environment? Happy to PR if you think it makes sense, but I can go either way on it. |
@scottyeck which one? Seems like the reports are mixed on the babel plugin mentioned in the issue + that'd only help with imports which wouldn't solve the reproduction you added above. |
@scottyeck @KyleAMathews As I'm reading through that thread, I saw that one option is to do I guess this is complex and I see why that might be their best solution, but it's a little funky for me because my app doesn't depend on react-hot-loader directly for the helper function. So I tried Although I hope you can still get some sort of Babel plugin to work so users of Gatsby don't have to know all this ;) |
Thanks for following up everybody. Yucko. @KyleAMathews Yeah - I'd agree that the babel-plugin doesn't quite feel like the way to go. Regardless, thanks a TON for doing the research and tracking this down. Made life a heck of a lot easier. @bradwestfall Yup - we're doing the same as a workaround for the time being. As it mentions in the docs, this is addressed in react-hot-loader's We could publish a utility package - something like I'm happy to take the lead on this guy if we feel it's helpful. |
@scottyeck as far as having the two ways for dev and production, I'm just going to short circuit it:
Thank you all for your help tracking this down |
@scottyeck that looks great. A little utility lib + a docs page explaining what the heck is going on that we can direct people to should suffice for now. Glad it's sorta working now! :-) |
Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help! |
If anyone has issue with this... it's important that you explicitly specify |
Description
I have a few components that rely on
React.Children.map
to eventually comparechild.type
to the expected child component. This code works fine in all my React projects that use ordinary react (like Create React App), but doesn't work in Gatsby or NextJS (perhaps something to do with SSR) All my code is on the same version of React and Node (listed below)Here is a basic example of a "compound components" pattern where
<Foo>
expects<Bar>
as it's child. In my CRA apps I get "Bar: world" as the end result (which is correct), but with Gatsby I get "not same" which shows a difference in how the two environments evaluate this codechild.type === Bar
In a quick twitter chat with gatsby, I was asked if refreshing made a difference, it does not
Environment
Gatsby version: 1.1.28
Node.js version: 8.2.1
Operating System: macOS High Siera
Actual result
child.type
is not===
to what it should be equal toExpected behavior
It should be equal
Steps to reproduce
Copy the above demo code and implement with
<Foo><Bar /></Foo>
The text was updated successfully, but these errors were encountered: