Introduction

In this guide, we will learn how to use if and else statements in your views. This is useful when you want to conditionally render some content based on a certain condition.

Basic usage

Here’s an example of how to use if and else statements in your views to display a message or a chart depending on whether the query result is empty or not.

<View>
    {#if param('user_token')}
        <Chart data={...} />
    {:else}
        <Text>No data available</Text>
    {/if}
</View>

Using query results as conditions

You can also use any query result to conditionally render content. To do so, use the await syntax to wait for the query to resolve before using the result.

<View>
    {#await runQuery('sample')}
        <Text>Loading...</Text>
    {:then data}
        {#if data.length > 0}
            <Chart data={...} />
        {:else}
            <Text>No data available</Text>
        {/if}
    {:catch error}
        <Text>Error: {error.message}</Text>
    {/await}
</View>

Learn more about how to use await in the Await documentation.