Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda