-
<div>
{showHeader && <Header />}
<Content />
</div> This example is from react docs. If showHeader is a reactive value (would be a function in solid) and it changes, solid knows it needs to do something. React would re-render the whole component and then do vdom diffing/reconciliation. But solid does not re-render, it's supposed to do fine-grained updates, which would mean what here? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
If I'm not mistaken, each JSX curly bracket expression is a computation. You can think about it as a small function that is going to be re-evaluated each time a reactive value (that was accessed inside it) changes. const Comp = () => {
const [showHeader, setShowHeader] = createSignal(false);
return (
<div>
{showHeader() && <Header />}
<Content />
</div>
);
};
{showHeader() && <Header />} After re-evaluating, Solid will make the required DOM manipulations to reflect the new DOM state. Note the use of Each curly bracket expression is re-evaluated independently. const Comp = () => {
const [showHeader, setShowHeader] = createSignal(false);
const [showFooter, setShowFooter] = createSignal(false);
return (
<div>
{showHeader() && <Header />}
<Content />
{showFooter() && <Footer />}
</div>
);
}; In this case, if you change Solid doesn't need a vDOM diff because it knows exactly what parts of the DOM need to be modified thanks to the fine-grained reactivity system. |
Beta Was this translation helpful? Give feedback.
-
@luisherranz is correct. Although conditionals have some additional logic. The way I handle this specific case in Solid is memoized guards. Your example compiles down to basically: const _el$ = _tmpl$.cloneNode(true);
const _c$ = createMemo(() => !!showHeader());
insert(_el$, () => _c$() && createComponent(Header, {}), null);
insert(_el$, createComponent(Content, {}), null);
return _el$; By hoisting the condition the insert doesn't re-run again unless the boolean condition changes. So we never recreate release/recreate Header unless In this scenario you can picture function insert(el, accessor, nextSibling) {
createEffect(() => el.insertBefore(accessor(), nextSibling);
} Although there is a lot more logic in there. Does that make sense? |
Beta Was this translation helpful? Give feedback.
@luisherranz is correct. Although conditionals have some additional logic. The way I handle this specific case in Solid is memoized guards.
Your example compiles down to basically:
By hoisting the condition the insert doesn't re-run again unless the boolean condition changes. So we never recreate release/recreate Header unless
showHeader
actually changes from true to false or vice versa. That effect doesn't rerun.In this scenario you can picture
insert
like: