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

160
Vistas
understanding logical operators and react rendering

please help me in understanding this : i am doing a conditional rendering in react-native something like this

{state.sign && (<Image style={{height:200,width:100}} source{{uri:state.sign}})}

it renders with no issue i have this in stacknavigation and when i try to navigate to the root with navigation.popToTop()

this throws an error . However when i change it to

{ state.sign ? (<Image style={{height:200,width:100}} source{{uri:state.sign}}): null}

it works . there is something that i am not understanding. something where "&&" is continuously monitored and "?:" is processed once and dusted?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Probably you forgot to close the tag?

  <Image
        style={styles.tinyLogo}
        source={{
          uri: 'https://reactnative.dev/img/tiny_logo.png',
        }}
      />
about 4 years ago · Juan Pablo Isaza Denunciar

0

For the most part there's not much of a difference between the two, but one use case where it matters is when it's the return value of rendering. React components can render valid JSX, or null to indicate nothing is to be rendered, and booleans are ignored. Returning undefined isn't allowed.

Conditional Rendering

Inline If with Logical && Operator

This is used if you want to conditionally include some JSX with the rest of the rendered JSX.

Example

return (
  <>
    <h1>A Header</h1>
    <div>Some text on the page</div>
    {state.sign && (
      <Image
        style={{height: 200, width: 100}}
        source{{ uri:state.sign }}
      />
    )}
  </>
);

Inline If-Else with Ternary Operator

This is used if you want to conditionally render one JSX or another JSX along with the rest of the rendered JSX.

Example:

return (
  <>
    <h1>A Header</h1>
    <div>Some text on the page</div>
    {state.sign ? (
      <Image
        style={{height: 200, width: 100}}
        source{{ uri:state.sign }}
      />
    ) : (
      <div>There is nothing to see here!</div>
    )}
  </>
);

Preventing Component from rendering

In rare cases you might want a component to hide itself even though it was rendered by another component. To do this return null instead of its render output.

Here's where the difference matters if what you are trying to conditionally render is the return value of a component.

Invalid because it doesn't return null to render nothing:

return state.sign && (
  <Image
    style={{height: 200, width: 100}}
    source{{ uri:state.sign }}
  />
);

Valid:

return state.sign ? (
  <Image
    style={{height: 200, width: 100}}
    source{{ uri:state.sign }}
  />
) : null;
about 4 years ago · Juan Pablo Isaza 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