Pass props from Component to props.children
Things that I have tried and do not work:
8 Replies
Since the component
Child
is already called when you do <Child />
you cannot pass any props
to it.
You could instead pass a function
and then props.children
would be that function that you can call and pass the props
.If you only want to change class of the
Child
, which returns the html element, you can change it no problem with this:
https://docs.solidjs.com/reference/component-apis/children
like here
https://www.solidjs.com/tutorial/props_children
Also what's stopping you from doing:
SolidJS
Solid is a purely reactive library. It was designed from the ground up with a reactive core. It's influenced by reactive principles developed by previous libraries.
with the
children
api I linked you can change only html properties, not the props
of function componentI've always done it like this.
Here's a custom button that you can use as a child.
Then you can customize this child in a parent like this.
Hope that helps.
As said before.
The children are already rendered when the parent function is executed. There is no cloneElement like in react.
So you have to figure out a way to change the props from within the children.
One way would be a context where you pass props in the parent and consume the context in the Child component(s).
And what also comes in handy is the Dynamic component which gives you the ability to setup all the props for the Child component and also gives users the ability to use any html element or component through the as prop.
What about child accepting an class and returning an element?
I've run into a similar issue.
Dynamic
but that's fine toocombining function-as-child and dynamic should work?