Skip to content
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

Conditional Rendering #6

Closed
andymans opened this issue Jun 20, 2019 · 8 comments
Closed

Conditional Rendering #6

andymans opened this issue Jun 20, 2019 · 8 comments
Labels
good first issue Good for newcomers question Further information is requested

Comments

@andymans
Copy link

Does an ${if...} ${if...else} exist?

@luwes
Copy link
Owner

luwes commented Jun 20, 2019

There are no extra tags but yes this is possible in the expressions via plain JS.

I put together an example here.
https://codesandbox.io/s/vigilant-elgamal-nx8nn

const template = () => {
  return html`
    <div style=${style}>
      Sinuous
      ${() =>
        count() >= 3
          ? html`<b> count ${count}</b>`
          : html`<b> cool</b>`
	   }
    </div>
  `;
};

@luwes luwes added the question Further information is requested label Jun 20, 2019
@andymans
Copy link
Author

Thanks!

@luwes luwes added the good first issue Good for newcomers label Jun 30, 2019
@dpamp
Copy link

dpamp commented Oct 11, 2019

Does an ${if...} ${if...else} exist?

I modified the example with if and return statements and works too

const template = () => {
  return html`
    <div style=${style}>
      Sinuous
      ${() =>{
        if (count() >= 3) {
         return html`
         <b> count ${count}</b>
       `
        } else {          
          return html`
              <b> cool</b>
            `
        }
      } 
      }
    </div>
  `;

@ryansolid
Copy link

It is worth pointing out in all these cases that as count goes to 4, 5, 6, 7, 8 ... You will redoing the work and recreating that template over an over. For a simple case like this that is no problem, but if you have to be careful with conditions and possibly need to hoist the condition if you don't want the parent computed to run over and over.

@theSherwood
Copy link
Contributor

What does this look like with memoization?

@luwes
Copy link
Owner

luwes commented Jan 18, 2020

something like this would work https://codesandbox.io/s/thirsty-ptolemy-89qbi

for this example that is definitely overkill I think and a simple hoist like @ryansolid suggested would be better. updated the demo with an example of this

https://codesandbox.io/s/vigilant-elgamal-nx8nn

@luwes
Copy link
Owner

luwes commented Jan 18, 2020

found a slightly cleaner solution that retains the locality:
https://codesandbox.io/s/elegant-neumann-mpn0u

there might be an even better one, not sure.

      html`${when(
        () => count() >= 3,
        cond =>
          cond
            ? console.log("render count") ||
              html`
                <b> count ${count}</b>
              `
            : console.log("render cool") ||
              html`
                <b> cool</b>
              `
      )}`
const when = (condition, view) => {
  const memoView = memo((...args) => root(() => view(...args)));
  return () => memoView(condition());
};

@nettybun
Copy link
Contributor

To any future devs, this might be useful as well: #115 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants