Skip to content

How does conditional rendering work? #545

Answered by ryansolid
bughit asked this question in Q&A
Discussion options

You must be logged in to vote

@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 showHeader actually changes from true to false or vice versa. That effect doesn't rerun.

In this scenario you can picture insert like:

function insert(el, a…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
2 replies
@bughit
Comment options

@luisherranz
Comment options

Comment options

You must be logged in to vote
2 replies
@bughit
Comment options

@ryansolid
Comment options

Answer selected by bughit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants