Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
Printing state value in React which is boolean doesn't get printed by String Interpolation
import React, { Component } from 'react';

class ButtonStatus1 extends Component {

  constructor(props){
    super(props);
    this.state = {
      a: "false",
      b: false
    }
  }

  render(){
    return (
      <div>
        <h1>a: {this.state.a}, b: {this.state.b}</h1>
      </div>
    );
  }
}

export default ButtonStatus1;

Output:

enter image description here

Expected:

false, false

String Interpolation, as far as I know, will always output in form of string, whatever the input is. So, in that scenario, even if this.state.b was a boolean - false, the output should have been printed. If the value is there -- just because it was boolean, why is it not printed. Isn't this strange??

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

{} is not just string interpolation

It is a JSX syntax to inform that the expression inside it needs to be evaluated

But there are some specific values for which nothing will be rendered, like

<div />

<div></div>

<div>{false}</div>

<div>{null}</div>

<div>{undefined}</div>

<div>{true}</div>

and booleans are often used for conditional rendering like

{array.length > 0 && array.map(...)}

if array.length > 0 is false, rendering false is not desirable

React docs


btw, you can convert the booleans to strings if you want to render them (false.toString())

about 4 years ago · Santiago Trujillo Report

0

JSX is executing an expression inside of {}, it's not string interpolation.

If React DID print booleans/nulls/undefined, it would mean you could not do short circuits like:

isTrue() && <p>hello world</p>

Instead of rendering nothing if isTrue() equals false, it would always render false in the DOM. This would make conditional rendering much more cumbersome.

about 4 years ago · Santiago Trujillo Report

0

Into React world return false has the same result as return null. So string interpolation it is simple function that return NOT only strings. It can be any rendered JSX component. That's why false rendered as empty value. You should use

String(this.state.b);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!