How to check boolean field value in React from Sanity schema?

7 replies
Last updated: Jan 28, 2026
I have a boolean set up in my schema type for certain blog posts - If I want to determine in my react code whether or not this boolean is true or false, how might I go about checking it to put out one message vs another?
AI Update

You can check the boolean field value in your React code by fetching it from Sanity and using conditional rendering. Here's how:

1. Query the boolean field with GROQ

When fetching your blog post data, make sure to include the boolean field in your query:

import { createClient } from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'production',
  apiVersion: '2024-01-01',
  useCdn: true,
})

// Fetch your blog post with the boolean field
const query = `*[_type == "blogPost" && _id == $postId][0]{
  title,
  content,
  yourBooleanField
}`

const post = await client.fetch(query, { postId: 'your-post-id' })

2. Use conditional rendering in React

Once you have the data, you can use standard JavaScript conditional logic:

function BlogPost({ post }) {
  return (
    <div>
      <h1>{post.title}</h1>
      
      {post.yourBooleanField ? (
        <p>This message shows when the boolean is true</p>
      ) : (
        <p>This message shows when the boolean is false</p>
      )}
      
      {/* Or using && for single condition */}
      {post.yourBooleanField && <p>Only shows when true</p>}
    </div>
  )
}

3. If using Next.js

If you're working with Next.js, you might use the sanityFetch helper from next-sanity:

import { sanityFetch } from '@/sanity/lib/client'

export default async function BlogPostPage({ params }) {
  const post = await sanityFetch({
    query: `*[_type == "blogPost" && slug.current == $slug][0]{
      title,
      yourBooleanField
    }`,
    params: { slug: params.slug }
  })
  
  return (
    <div>
      {post.yourBooleanField ? 
        <div>Boolean is true!</div> : 
        <div>Boolean is false!</div>
      }
    </div>
  )
}

The key is to include your boolean field in your GROQ query, and then use standard JavaScript/React conditional operators (? :, &&, or if/else) to render different content based on its value.

Show original thread
7 replies

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?