-
-
Notifications
You must be signed in to change notification settings - Fork 937
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
Possible to access child props? #222
Comments
Are the <TabBar>
<Tab order='First'>
<MyComp1 />
</Tab>
<Tab order='Second'>
<MyComp2 />
</Tab>
</TabBar>
// define Tab as:
function Tab(props) {
return props;
} Now the props.children in |
Hmm, that's a good idea. The original could accept any component, and most of the framework functioned on a similar approach, so I more or less need to rebuild the thing completely. My approach so far has been essentially the same, except with non-JSX wrapper functions and POJOs. Working with Solid is very interesting, because it looks so much like React, and yet is fundimentally so very different. Thanks for the ideas! |
@ryansolid const group= (
<Parent>
<Child/>
<Child/>
</Parent>
);
const single = <Child/>; |
@mduclehcm If the Parent doesn't need to know what it's children's props or structure is, that will work as is. If it does, you'll need to wrap it somehow. Something like: <Parent>
<With first='prop' second={prop}><Child/></With> // Where With = (props) => props; basically
// or, you could do something like
<Child first='prop' second={prop} subordinate/>
</Parent>
// Where <Child> then has
if (props.subordinate) { return [props, <Child_Rendered_JSX/>]; }
else { return <Child_Rendered_JSX/>; }
// which then lets parent see props, whilst still letting it be used elsewhere. Problem with this is you'd have to
include the props.subordinate check on everything that could be used in Parent, which is no good. Also, an idea I've been exploring is extracting the logic out of JSX and into a function, something like createParent({...propsSpecificToParent}, [
[{...propsTheParentAndChildNeedToKnowAbout}, Child({...propsSpecificToChild})]
]); To maybe leave using JSX just to where HTML actually needs to be returned? I'm not sure if this is a good idea though, or just confusing. |
Coming from other systems like React or Custom Elements, this is actually one area that can be more tricky. I've had success using I wonder if there's a way to improve this for plain Solid though?
|
There are patterns around using The problem here is it requires an intermediate representation. My usually recommendation is if specialized children return your own like the way The basic problem isn't slotting at all. That's easy with JSX. The problem is the desire to have parents augment their children after the fact, which isn't very declarative of a pattern generally. In VDOM libraries you can do this with an abstraction before you do any rendering. We don't have that regardless of how we do the slotting. You still need a way to get the slots. And we do have that already. |
I'm not sure how "it won't help". The idea is that these intermediate representations are extra cognitive overhead for the common cases that the other abstractions guarantee without the overhead. When you read someone's code, and you see I think a built in solution would be nice, although I'm not yet sure what it would look like. I'm thinking it might be context based (or something like it). F.e. maybe we can have a context provider like function CompA() {
return <div>
<p>Distributed content:</p>
<Slot name="foo" />
</div>
} function CompB() {
return <CompA>
<Distribute slot="foo">
<p>This content gets distributed.</p>
</Distribute>
</CompA>
} I don't know if that's the best format, but it provides a standard. |
How do we slot to a specific location? For example, if we can't inspect that |
Here's how I would do it with current Solid: import { render } from "solid-js/web";
function Nav(props) {
return <div style="border: 2px solid deeppink">
{props.children ?
<div>
<p>Distributed content: </p>
{props.children}
</div>
:
<div>
<p>Default content.</p>
</div>
}
</div>
}
function App() {
return <>
<Nav />
<Nav>
<p>This is distributed.</p>
</Nav>
</>
}
render(() => <App />, document.getElementById("app")); If we need more than one "slot" in a component, then I'd use props like this: import { render } from "solid-js/web";
function Nav(props) {
return <div style="border: 2px solid deeppink">
{props.foo ?
<div>
<p>Distributed content (foo): </p>
{props.foo}
</div>
:
<div>
<p>Default content (foo).</p>
</div>
}
{props.bar ?
<div>
<p>Distributed content (bar): </p>
{props.bar}
</div>
:
<div>
<p>Default content (bar).</p>
</div>
}
</div>
}
function App() {
return <>
<Nav />
<Nav
foo={<p>This is distributed (foo).</p>}
bar={<p>This is distributed (bar).</p>}
/>
</>
}
render(() => <App />, document.getElementById("app")); |
I think that's what you meant by "easy with JSX" right @ryansolid? I think that's the best way to go right now. I'm not sure if the return <>
<Nav />
<Nav>
<Distribute to="foo"><p>This is distributed (foo).</p></Distribute>
<Distribute to="bar"><p>This is distributed (bar).</p></Distribute>
</Nav>
</> which is closer to what the the original post was going for. Keeping "slotted" children in the actual children area is what is not easy. |
In case it helps, the structure for tabs in @Brendan-csel's solid-bootstrap (ported from react-bootstrap) is: <Tabs defaultActiveKey="profile">
<Tab eventKey="home" title="Home">
<SomeContent/>
</Tab>
<Tab eventKey="profile" title="Profile">
<SomeContent/>
</Tab>
<Tab eventKey="contact" title="Contact">
<SomeContent/>
</Tab>
</Tabs> Interestingly, it is using the |
Would it be possible to allow a |
Hello again. Currently working on a prototype which is a port of some features out of a React app. There are a lot (honestly, most) of container components that organize or present stuff based on props of their children, for instance:
Which lays out the headers for each tab based on the child's
tab
prop. In Solid, all components seem to be compiled away completely and as such there doesn't seem to be a way to access child props. Is there any other mechanism by which it's possible for a component to know about it's internal structure like this?The text was updated successfully, but these errors were encountered: