#Can't use "if" in html
31 messages · Page 1 of 1 (latest)
https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator works the same in Astro
It doesn't allow me to do declarations or etc.
You can wrap your statements in a function
<body>
{
()=>{
if (fancy){
return <h1>Fancy</h1>
} else {
return <h1>Bad</h1>
}
}
}
</body>
Wow, that's great
Isn't this just the conditional render from above with extra steps, any benefits on this?
I switch to this when I start using a lot of ternaries, this is more readable for me
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
{
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
Not at all, you created the thread!
This is not supported, you can't use async functions inside the jsx Thanks for the correction
It does work actually
Hmm
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
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 />;
</>
)
}
}
}
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 />
</>
)
}
}
}