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

144
Vistas
React cant use useParams values

So my code was working with no issues until i added useParams

[...]

const promiseDelay = () => {
    const E_COMMERCE_API = "https://mocki.io/v1/027ebfda-9bd8-4b53-ae33-ab41586d773a";
    return new Promise((resolve, reject) => {
        setTimeout(() => resolve(fetch(E_COMMERCE_API)), 1000);
    });
};

export default function ItemDetailContainer() {
    const { itemId } = useParams();
    const [dataToShow, setDataToShow] = useState([]);

    useEffect(() => {
        promiseDelay()
            .then((response) => response.json())
            .then((data) => {
                const aux = data.find((element) => {
                    console.log(itemId); //<--- 2
                    // return element.id === itemId; <--- if i uncomment, i get no data
                    return element.id === 2; // with this line, everything works just fine
                });
                setDataToShow(aux);
            });
    }, []);

    [...]
}

Apparently even if itemId its 2 its not the same and wrecks everything.

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

0

It's probably because the param is a string when it's parsed from the URL. The useParams hook doesn't know to parse it to a number.

So element.id === Number(itemId) should fix it.

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