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

Error while conditional rendering of state value using ternary operator inside jsx in Reactjs 18.0

import "./styles.css";
import { useState } from "react";

export default function App() {
  const [points, setPoints] = useState({ playerOne: 0, playerTwo: 0 });
  const [state, setState] = useState(true);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h3> value is {points.playerOne} & {points.playerTwo} </h3>
      <h3> points of player one is {state === true ? 1 : 2} </h3>
      <h3> points of player one is {state === true ? {points.playerOne} : {points.playerTwo}} </h3>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

I can’t understand why I am facing an error due to the third h3 tag

<h3> points of player one is {state === true ? {points.playerOne} : {points.playerTwo}} </h3>

if I comment this above line of code then output is rendering fine

Use the link to run the code — https://codesandbox.io/s/silly-grass-wdoq68?file=/src/App.js

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

please help!

>Solution :

Remove the curly braces in Conditional (ternary) operator Do this:

  <h3> points of player one is {state  ? points.playerOne : points.playerTwo} </h3>

https://codesandbox.io/s/wizardly-voice-crbtjp

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