Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditional rendering JSX

I have some lines of JSX that I want to show/hide when I click on a button.

I have initialized the state as such :

const [shown, setShown] = useState(true);

And this is the JSX :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      <div className="question--section">
        <div className="question--count">
          <span>Question {props.class[currentQuestion].id} </span>
          <h1>{props.class[currentQuestion].questionText}</h1>
        </div>
      </div>

I have tried to to do this :

{shown ? (
     <div className="question--section">
        <div className="question--count">
          <span>Question {props.class[currentQuestion].id} </span>
          <h1>{props.class[currentQuestion].questionText}</h1>
        </div>
      </div>) :
       (<div>Empty</div>)}

It doesn’t work. How should I approach this? ( Switching the state should hide/show the JSX)

The whole component :

import React from "react";
import { useState } from "react";

const Quiz = (props) => {
  const [currentQuestion, setCurrentQuestion] = useState(0);

  const [shown, setShown] = useState(true);
  
  const handleAnswerButtonClick = () => {
    if (currentQuestion + 1 < props.class.length) {
      setCurrentQuestion((prevQuestion) => prevQuestion + 1);
    } else {
      alert("End of the quiz!");
    }
  };
  return (
    <div className="container quiz--container">
      <button>Κεφάλαιο 1</button>
      {shown ? (
      <div>
        <h1>Κεφάλαιο {props.id}</h1>
        <div className="question--section">
          <div className="question--count">
            <span>Question {props.class[currentQuestion].id} </span>
            <h1>{props.class[currentQuestion].questionText}</h1>
          </div>
        </div>
        <div className="answer-section">
          {props.class[currentQuestion].answers.map((answer) => (
            <button onClick={handleAnswerButtonClick}>{answer.answerText}</button>
          ))}
        </div>) : 
        (<div>Empty</div>)}
  
    </div>
  );
};

export default Quiz;

>Solution :

You are missing a closing </div> before the :.

It should be this:

<div className="container quiz--container">
      <button>Κεφάλαιο 1</button>
      {shown ? (
      <div>
        <h1>Κεφάλαιο {props.id}</h1>
        <div className="question--section">
          <div className="question--count">
            <span>Question {props.class[currentQuestion].id} </span>
            <h1>{props.class[currentQuestion].questionText}</h1>
          </div>
        </div>
        <div className="answer-section">
          {props.class[currentQuestion].answers.map((answer) => (
            <button onClick={handleAnswerButtonClick}>{answer.answerText}</button>
          ))}
        </div>
        </div>) : 
        (<div>Empty</div>)}
    </div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading