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

NavLink - Add a functionAsChild boolean prop to use children prop as a function #91

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Aetherall
Copy link

This will allow easy conditional rendering and will integrate well with many CSS-in-JS technologies ! :)

Usage:

const SuperMenu = ({classes}) => (
	<NavLink to={'/space'} functionAsChild>
		{ active => (
			<FancyButton className={active ? classes.rainbow : classes.gray}>
				 { active ? "You're in space now! Whooaaa" : 'Go to space !' }
			</FancyButton>
		)}
	</NavLink>
)

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 !

@faceyspacey
Copy link
Owner

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?

@Aetherall
Copy link
Author

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

@faceyspacey
Copy link
Owner

@Aetherall
Copy link
Author

Aetherall commented Mar 22, 2018

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 ?

@faceyspacey
Copy link
Owner

Log it and see what else we can check for. It’s fine if it’s not in the public api

@Aetherall
Copy link
Author

Aetherall commented Mar 22, 2018

Seems like there is plenty of possibilities !

image

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 !

@Aetherall
Copy link
Author

@faceyspacey Do you have an Idea on how to reduce the cognitive complexity of the PR ?

@faceyspacey
Copy link
Owner

faceyspacey commented Mar 24, 2018

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:

https://github.com/facebook/react/blob/b77b12311f0c66aad9b50f805c53dcc05d2ea75c/packages/react/src/ReactChildren.js#L118-L127

https://github.com/facebook/react/blob/f57d963ccea845e941f07e74c55920cd852dfc7b/packages/shared/ReactSymbols.js

@Aetherall
Copy link
Author

Aetherall commented Mar 24, 2018

Or maybe we could use the React dedicated function to check if it is a react component ?

image

But why not simply use typeof children === 'function' ? The only truthy case to me is when the children is a function ?

@faceyspacey
Copy link
Owner

I dunno. I'm not in it like you are.

@Aetherall
Copy link
Author

From what I tried, The typeof implementation works, so now I need to decrease the code complexity
Can you help me with this ?

@faceyspacey
Copy link
Owner

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.

@Aetherall
Copy link
Author

Great thanks !!

@sibnerian
Copy link

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. ✌️

@ScriptedAlchemy
Copy link
Collaborator

@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

@ScriptedAlchemy
Copy link
Collaborator

@sibnerian bump

@sibnerian
Copy link

@ScriptedAlchemy sorry for the delay. I updated my PR, see #112 .

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

Successfully merging this pull request may close these issues.

None yet

4 participants