#Can't use "if" in html

31 messages · Page 1 of 1 (latest)

vital anvil
#

Yesterday I was trying to use "if" in a html inside of curly braces, but it didn't allow me. So had to do some hacky stuff, took the item in an array and used, filter + map combination to get what I want. I'm sure I've done something wrong, there probably is a better way. Do you know how can I do it?

faint sinew
vital anvil
#

It doesn't allow me to do declarations or etc.

flat patio
#

You can wrap your statements in a function

#
<body>
  {
    ()=>{
      if (fancy){
    return <h1>Fancy</h1>
      } else {
    return <h1>Bad</h1>
      }
    }
  }
</body>
vital anvil
#

Wow, that's great

drifting scroll
flat patio
#

I switch to this when I start using a lot of ternaries, this is more readable for me

vital anvil
#

My take on this was, inside the if brackets, I can do declarations like const { Content } = await page.render()

#
{
    async () => {
        if (toRender !== undefined) {
            const { Content } = await toRender.render();
            return <Content />;
        }
    }
}
#

This worked like a charm

#

Thanks guys

vital anvil
#
{
    async () => {
        if (toRender !== undefined) {

            const { data } = toRender;

            () => {
                if (isIndex) {
                    return <h1>{ data.title }</h1>;
                }
                return <h1>{ data.title }<span class="primary"> posts</span></h1>;
            }

            const { Content } = await toRender.render();
            return <Content />;
        }
    }
}

#

This one doesn't show h1 tags

#
{
    async () => {
        if (toRender !== undefined) {

            const { data } = toRender;

            isIndex ? <h1>{ data.title }</h1> : <h1>{ data.title }<span class="primary"> posts</span></h1>

            const { Content } = await toRender.render();
            return <Content />;
        }
    }
}
#

This doesn't either

#
{
    async () => {
        if (toRender !== undefined) {

            const { data } = toRender;

            { isIndex ? <h1>{ data.title }</h1> : <h1>{ data.title }<span class="primary"> posts</span></h1> }

            const { Content } = await toRender.render();
            return <Content />;
        }
    }
}
#

Or this

#

Sorry if I'm spamming, just trying to give as much details I can

flat patio
flat patio
vital anvil
#

It does work actually

flat patio
#

Hmm

vital anvil
#

I can get the content

#

I'm having difficulties with the isIndex logic here

#

Guess I have to rewrite if (toRender !== undefinded) part again

#

Didn't want to, since it's copy & paste, which is bad practise

flat patio
#

Ahh that's actually not valid jsx

#

You could wrap everything after the return

#

This should work

{
    async () => {
        if (toRender !== undefined) {

            const { data } = toRender;
            const { Content } = await toRender.render();
            return (
            <>
              { isIndex ? <h1>{ data.title }</h1> : <h1>{ data.title }<span class="primary"> posts</span></h1> }
              <Content />;
            </>
            )
        }
    }
}
vital anvil
#

Following worked just fine, thanks for helping

{
    async () => {
        if (toRender !== undefined) {

            const { data } = toRender;
            const { Content } = await toRender.render();

            return (
            <>
              { isIndex ? <h1>{ data.title }<span class="primary"> posts</span></h1> : <h1>{ data.title }</h1> }
              <Content />
            </>
            )
        }
    }
}