Open Aetherall opened 6 years ago
very nice, for the next major version I'll try to figure out if we can do it automatically. ..cant we just check typeof children
or something similar?
I don't think so since it would return function
.. and children instanceof React.Component
don't work neither, maybe checking a react specific property on the children object but it seems kind of hacky to me
it's fine, here's an example:
https://github.com/acdlite/recompose/blob/master/src/packages/recompose/isClassComponent.js
and what if the children is not a class component but a function component ?
const FunctionComponent = () => <div>Hello !<div>
const Menu = () => (
<nav>
<Navlink to={'/somewhere'}><FunctionComponent/></Navlink>
</nav>
)
Does the function still works here ?
Log it and see what else we can check for. It’s fine if it’s not in the public api
Seems like there is plenty of possibilities !
We can do a
{ children.reactRelatedThing === undefined ? children(active) : children }
Ooh seems like I was wrong ! typeof does return 'object' when the children is a react component !
@faceyspacey Do you have an Idea on how to reduce the cognitive complexity of the PR ?
First add a 3rd check for an array of components.
Then, the best way probably is to check the children.$$typeof
Symbol. My first instinct was to check that _owner
is defined, but if the Symbol
is one we can figure out, which it of course is, and if it's very descriptive, that's probably the most "professional" way to do this.
Now that said, there likely is other symbol types, and the best thing we can do is look at how React handles these checks, which they inevitably do:
Or maybe we could use the React dedicated function to check if it is a react component ?
But why not simply use typeof children === 'function'
? The only truthy case to me is when the children is a function ?
I dunno. I'm not in it like you are.
From what I tried, The typeof implementation works, so now I need to decrease the code complexity Can you help me with this ?
I don't have any time currently. I'd get it working for your purposes, fork it, post any info you can, and I'll implement this in the next major version.
Great thanks !!
Thanks for making this PR @Aetherall, super helpful. I just hit this in my own project and made a similar branch, except I check the count
of the children as well.
I'm going to make a PR just to see what the "cognitive complexity" is. ✌️
@sibnerian can you recheck this PR against the latest RFR2 and the latest released version of this package. If all is good I’ll merge and deploy
@sibnerian bump
@ScriptedAlchemy sorry for the delay. I updated my PR, see #112 .
This will allow easy conditional rendering and will integrate well with many CSS-in-JS technologies ! :)
Usage:
If there is a super fancy way to distinguish normal childs and functionsAsChilds, it would be better to use that instead of having a functionAsChild prop
But for the time being I hope this will help somebody !