-
Hello! I am trying to make my library to support SolidJs. But I am facing an obstacle and cannot find it. I checked all the relative articles and Q&A, but all of them are ended up suggesting a different way of fixing the issue. Maybe what I am using Solid is an anti-pattern, but I still cannot understand why Solid does not support this use case (if I am right). My example is about passing additional props from the parent component to the children( multiple child ). And they should be different data based on its order. Here is my example <Parent width={160}>
<Child ratio={5} > child1 </Child>
<Child ratio={2} > child2 </Child>
<Child ratio={1} > child3 </Child>
</Parent> after ratio from the children is given, it should be summed and create DOM element based on parent's width prop. expecting result: <div width="width:160px">
<div style="width:100px">child1</div>
<div style="width:40px">child2</div>
<div style="width:20px">child3</div>
</div> But I cannot achieve this because There is no way to receive both from Parent and Child. I tried to use Context, Dynamic, passing props, and may other trials, but still could not figure out how to make it happen. To experiment, I made this code: import './App.css';
import { createContext, useContext, createEffect, createSignal, For, children as Children } from 'solid-js';
export const UserContext = createContext(0);
function App() {
return (
<>
<ParentComponent>
<Child width={3} />
<Child width={2} />
<Child width={1} />
</ParentComponent>
</>
);
}
export default App;
export const ParentComponent = props => {
return (
<For each={Children(() => props.children)()}>
{(child, idx) => {
const c = Children(() => child);
const existingProps = c.props; // is there a way to pass existing child's props?
return (
<UserContext.Provider value={idx}>
---
<Dynamic component={c} idx={idx} />
<Dynamic component={Child} {...existingProps} idx={idx} />
---
</UserContext.Provider>
);
}}
</For>
);
};
export const Child = props => {
const idx = useContext(UserContext);
return (
<div>
I am child {props.idx ?? 'no-value'} with width {props.width ?? 'no-value'} and idx from context {idx}
</div>
);
}; and its result is like this:
As you see it, first option does not have props from Parent, the other one does not have props from Children. Please help me or at least let me know if it is impossible. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
https://playground.solidjs.com/anonymous/b9e09a8f-310a-466a-af35-dcda7bb07625 The pattern you're looking for might be this which solid-js already uses. <Switch>
<Match when={foo()}>is foo</Match>
<Match when={bar()}>is bar</Match>
<Match when={true}>just true</Match>
</Switch> solid/packages/solid/src/render/flow.ts Lines 144 to 234 in 2ae7d1d or solid router's route definition <Router>
<Route .../>
<Route .../>
<Route .../>
</Router> This pattern is so powerful and useful that to help with this pattern, @thetarnav made some helpers in https://github.com/solidjs-community/solid-primitives/tree/main/packages/jsx-tokenizer and is used by @bigmistqke in https://github.com/bigmistqke/solid-canvas |
Beta Was this translation helpful? Give feedback.
-
https://playground.solidjs.com/anonymous/74f7be1f-585c-4282-9280-79430f16ac56 Solution with context for reference. Click on each child to change their ratio too ! |
Beta Was this translation helpful? Give feedback.
-
I recently found out another pattern that is pretty handy, but it assumes the child component returns an html-element:
|
Beta Was this translation helpful? Give feedback.
https://playground.solidjs.com/anonymous/b9e09a8f-310a-466a-af35-dcda7bb07625
The pattern you're looking for might be this which solid-js already uses.
https://docs.solidjs.com/reference/components/switch-and-match
solid/packages/solid/src/render/flow.ts
Lines 144 to 234 in 2ae7d1d