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

186
Vistas
I wish to toggle on and off the display of my <p> tag

I want to toggle a <p> tag in react based on whether a link is an URL or not.

<p style={{color : "red", display : {isValid}}} >Invalid URL</p>

The function that determines the value of isValid is

const [isValid, setIsValid] = useState('none');

const isUriImage = function(uri) {
    console.log(isValid)
    setImage_url(uri.target.value);
    uri = uri.target.value;
    uri = uri.toString().split('?')[0];
    var parts = uri.split('.');
    var extension = parts[parts.length-1];
    var imageTypes = ['jpg','jpeg','tiff','png','gif','bmp']
    if(imageTypes.indexOf(extension) != -1) {
        setIsValid('none');   
        console.log("Valid")
    } else {
        setIsValid('');
        console.log("Not Valid")
    }
}

The code is working fine as I have confirmed it with the console.log statements, but my <p> tag's display is not being changed.

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

0

You are setting display property as and object with key isValid, instead assign isValid as variable directly

<p style={{ color: "red", display: isValid }} >Invalid URL</p>

Just to mention, in React way it would be better to not render p tag at all if you just want to hide it conditionally

Use boolean instead

const [isValid, setIsValid] = useState(false);

Render conditionally

{isValid && <p style={{ color: "red" }} >Invalid URL</p>}

In you scenario, you element display will be 'none' but it will still be in DOM. In above scenario it won't be rendered to DOM.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Just remove the extra {} around the isvalid variable.

<p style={{color : "red", display : isValid }} >Invalid URL</p>
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