> ## Documentation Index
> Fetch the complete documentation index at: https://learn.mintlify.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Conditional content

> Use the view component to show or hide content based on a user selection.

export const Quiz = ({question, answers, correctFeedback, incorrectFeedback}) => {
  const [selected, setSelected] = React.useState(null);
  const [checked, setChecked] = React.useState(false);
  const quizId = React.useId();
  const isCorrect = checked && answers[selected]?.correct;
  const reset = () => {
    setSelected(null);
    setChecked(false);
  };
  return <div className={"quiz-container" + (checked ? " quiz-checked" : "")}>
      <Badge color="green">Quiz</Badge>
      <p className="quiz-question" id={quizId + "-question"}>{question}</p>
      <div className="quiz-options" role="radiogroup" aria-labelledby={quizId + "-question"} aria-disabled={checked}>
        {answers.map((answer, i) => <label key={i} className={["quiz-option", !checked && selected === i ? "quiz-option-selected" : "", checked && answer.correct ? "quiz-option-correct" : "", checked && selected === i && !answer.correct ? "quiz-option-incorrect" : ""].filter(Boolean).join(" ")}>
            <input type="radio" name={"quiz-" + quizId} checked={selected === i} onChange={() => !checked && setSelected(i)} disabled={checked} />
            <span className="quiz-radio" />
            <span className="quiz-option-label">{answer.text}</span>
          </label>)}
      </div>
      {checked && <div className={"quiz-feedback " + (isCorrect ? "quiz-feedback-correct" : "quiz-feedback-incorrect")} role="status" aria-live="polite" aria-atomic="true">
          <span className="quiz-feedback-icon">{isCorrect ? "✓" : "✗"}</span>
          {isCorrect ? correctFeedback : incorrectFeedback}
        </div>}
      <div className="quiz-actions">
        {!checked ? <button className="quiz-btn quiz-btn-check" type="button" onClick={() => selected !== null && setChecked(true)} disabled={selected === null}>
            Check answer
          </button> : <button className="quiz-btn quiz-btn-reset" type="button" onClick={reset}>
            Try again
          </button>}
      </div>
    </div>;
};

The `<View>` component lets you conditionally show content based on a user's selection.

## What does `<View>` do?

`<View>` components create a multi-view dropdown that lets users switch between different versions of a page. Each `<View>` block has a `title` that identifies it in the dropdown. When a user selects a title, only that block's content displays.

````mdx Example of views theme={null}
<View title="JavaScript" icon="js">
  This content is only visible when JavaScript is selected.

  ```javascript
  console.log("Hello from JavaScript!");
  ```
</View>

<View title="Python" icon="python">
  This content is only visible when Python is selected.

  ```python
  print("Hello from Python!")
  ```
</View>
````

To see views in action, select JavaScript or Python from the dropdown above the table of contents and see how the following content changes.

<View title="JavaScript" icon="js">
  This content is only visible when JavaScript is selected.

  ```javascript theme={null}
  console.log("Hello from JavaScript!");
  ```
</View>

<View title="Python" icon="python">
  This content is only visible when Python is selected.

  ```python theme={null}
  print("Hello from Python!")
  ```
</View>

The table of contents also updates when a user switches views, showing only the headings within the selected view.

## How to choose between views, tabs, and code groups

These three components often seem interchangeable, but each fits a different situation:

* Use `<CodeGroup>` when only the code varies and the explanation is the same for all languages.
* Use `<Tabs>` for content that varies by framework, authentication method, or other user context.
* Use `<View>` when large amounts of content or multiple code samples vary by language or framework across an entire page.

The difference is scope. Tabs are localized, only one section of the page changes. Views affect the entire page and update the table of contents to match.

## When is `<View>` the best choice?

View components work best when:

* Your page covers a concept that plays out differently across multiple languages or frameworks.
* The structure and headings of the page differ meaningfully between languages (not just the code).
* You want users to set their language once and have it apply to everything on the page.

A typical use case is an SDK reference page where the JavaScript version uses promises and the Python version uses a different pattern entirely with different examples, explanations, and headings.

## When not to use `<View>`

Don't use views when only the code differs between options. That's what `<CodeGroup>` components are for, shared explanation, language-specific code.

Don't use views when procedures differ in a single section of a longer page. Tabs handle localized variation better, since they leave the rest of the page unchanged.

Don't use views when users are choosing between options to compare them. Views are for users who have already chosen their language or framework and want to see only what's relevant to them.

<Quiz
  question="A page documents an API concept in depth. The explanation is the same regardless of language, but each code example is in a different language. Which component should you use?"
  answers={[
{ text: "A view component because users can switch between language versions of the page.", correct: false },
{ text: "A code group component because only the code varies, so the explanation stays shared.", correct: true },
{ text: "Tabs because one tab per language with the explanation and code inside.", correct: false },
{ text: "Separate pages. One per language.", correct: false },
]}
  correctFeedback="Right. When the explanation is the same and only the code varies, a code group component is the right choice. Views are for when the page structure and headings differ meaningfully by language, not just the code snippets."
  incorrectFeedback="Views are for pages where the content differs significantly by language. When only the code changes, code group components keep the explanation shared and let users switch languages without duplicating prose."
/>

<Check>
  Next up: [Tables for structured comparisons](/courses/components/tables) — When grid layouts communicate relationships better than prose or cards.
</Check>
