S
SolidJS•16mo ago
aquarazorda

Is it possible to create fully functional component without JSX/TSX syntax?

I'm trying to use Solid with PureScript, but seems like when component gets compiled from TSX to JS, a lot of stuff is happening. Basically, is it possible somehow to make something like this work?
return Dynamic({
component: "div",
children: [
val(),
Dynamic({
component: "button",
children: "Test",
onClick: () => setVal("testVal")
})
]
});
return Dynamic({
component: "div",
children: [
val(),
Dynamic({
component: "button",
children: "Test",
onClick: () => setVal("testVal")
})
]
});
10 Replies
aquarazorda
aquarazorda•16mo ago
I think I figured it out.
export const dynamic = (component) => (props) => () => {
const { children, ...rest } = props;
return createComponent(
Dynamic,
({
component,
get children() {
return children?.map(c => typeof c === 'string' ? c : createMemo(() => c()))
},
...rest
}));
}
export const dynamic = (component) => (props) => () => {
const { children, ...rest } = props;
return createComponent(
Dynamic,
({
component,
get children() {
return children?.map(c => typeof c === 'string' ? c : createMemo(() => c()))
},
...rest
}));
}
this one works
lxsmnsyc
lxsmnsyc•16mo ago
destructuring breaks reactivity, I wouldn't recommend this
aquarazorda
aquarazorda•16mo ago
yup, changed it already, thanks @lxsmnsyc @lxsmnsyc can I create a fragment with Dynamic, if you know?
lxsmnsyc
lxsmnsyc•16mo ago
you'd have to use this:
function Fragment(props) {
return createMemo(() => props.children);
}
function Fragment(props) {
return createMemo(() => props.children);
}
aquarazorda
aquarazorda•16mo ago
hmm, then it means, instead of mapping that children and memoing it, I can simply return Fragment(props) in above component and it will work as expected perfect
aquarazorda
aquarazorda•16mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
aquarazorda
aquarazorda•16mo ago
Seems like this doesn't really do the justice and I'm missing some of stuff with this approach. Isn't there a way to somehow get the same output with plain js (maybe by using some internal functions) and if not, what can be the best approach in this case or even maybe there's absolutely no point of doing this and it's a generally a bad idea to use Solid with TypeScript. @ryansolid I'll tag you if you don't mind, I've watched couple of streams by you and haven't really got an answers for this question. I saw how you did something like this, but you didn't say what are the downsides with this and maybe you haven't thought about this kind of usage of Solid. With PureScript and React, it's very easy, since React's JSX gets transformed to JS without this extra stuff. Seems like using Dynamic as the main building block, doesn't really work, it does extra re-renders.. 😦
aquarazorda
aquarazorda•16mo ago
here's an example - https://playground.solidjs.com/anonymous/619ecfaa-e283-41c4-95a2-fa91fd4ff2d4 You can swap Counter with Counter2 and check in dev tools' Elements, how things are being re-rendered. There's also clear difference how they are being compiled. I've replicated this with PureScript and then figured out that it doesn't really work like this correctly.. :X
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
ryansolid
ryansolid•16mo ago
Hmm.. interestingly I see what you see in the dev tools.. However it is the same DOM elements It's more like they are being re-inserted in the same spot I guess It doesn't create extra DOM nodes Chrome doesn't even collapse the nodes in the inspector In general prop manipulation is where a lot of the work of the compiler comes in. If we didn't do this people would be having to handle this wrapping themselves on both sides to figure out what is a signal what isn't etc.. and ensure all their expressions that need to be wrapped are. Generally I don't recommend people using the standard JSX transform for this reason or Tagged Template Literals. It is more complicated. We do have functions to wrap props in DOM Expressions and we have a mergeProps function which is basically our equivalent to Object.assign to retain reactivity but like this isn't the experience I'd wish on anyone.
aquarazorda
aquarazorda•16mo ago
That's it, thanks a lot, That mergeProps solved my issues, now I can enjoy using Solid with PureScript. Thanks a lot, good luck and looking forward for updates, streams and all kinds of good stuff that you are doing, I appreciate that you replied.
Want results from more Discord servers?
Add your server
More Posts
Typescript type for generic html props passthrough?Looking to create a generic wrapper component that can take arbitrary HTML props (class, etc) withouDon't send component logic to client conditionally on data?Hey everyone! I'm trying to wrap my head around this use case and can't understand how one would do Adding aws-amplify libs adds 5s each time I change a line of codeSo after adding `"aws-amplify": "^5.0.15",` to my project, as well as ``` { find: "./rComponent is not updated when prop is changedI have a component "ShowDocument" with a resource inside, the component has a variables that needs tindex.html <script> tag doesn't work?Hi I'm using solidStart. The script tag in the index.html doesn't appear to be working. I add a condelay execution of createEffect.I have a use case where I want one of my createEffect to be called before another . Currently they Breaking out proxied Stores object as a signal (while retaining reactivity)Let's say I have an album object... ``` type Album = { id: string; artist: string; ratinSSR Hydration error in ChildRoute with Context set in ParentRoute and intermediate Context UpdateI have a ParentRoute that retrieves RouteData and uses that data to populate a Context ```tsx exporRight way of getting errors out of async context functionsI have a context that has methods that are async, the method itself on the context is sync, but thatAvoid Provider nestingI am going to have a lot of Providers in my App, and i did not want to have 5+ nesting around my rot