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

167
Views
react está tratando de representar la página incluso antes de que se obtenga el params.id, por lo que solo se ejecuta la declaración if cada vez

Quiero representar el código de reacción cuando la identificación en los parámetros coincida con la identificación en los datos, pero aquí reaccionar intenta representar el código incluso antes de que se ejecute props.match.params.id , por lo que solo se puede ejecutar la instrucción if. yo con una solución para resolver esto. Incluso he intentado usar useParams() pero no hace nada bueno.

 import data from'./data' import React from 'react' export default function Productdisplay(props) { const y= props.match.params.id const product=data.products.find((x) => x.id===y); //An error lies here if(!product){ return( <div>No product to display {}</div> ) } return( <> //Here lies the code I want to render when the ids of the url and data match but as React tries to render the code even before the params is being fetched only if statement is being rendered can you give me a solution for this </> ) }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo que quiere usar aquí es un useEffect que contiene sus condiciones/lógica que se ejecutará cuando cambien sus deficiencias. Aquí está Y. Luego almacene la salida de su lógica de useEffect en un estado real (useState) y no en una const. A partir de ahí, podrá activar el renderizado que espera.

 import React, { useState, useEffect } from 'react'; function Productdisplay(props) { const [products, setProducts] = useState(); useEffect(() => { const y = props ? .match ? .params ? .id; const findProducts = data.products.find((x) => x.id === y); if (findProducts !== products) { setProducts(products); } }, [data?.products, props?.match?.params?.id]); if(!products) return null; return ( <div> // ... </div> );

Te sugiero que leas esto https://reactjs.org/docs/hooks-effect.html

about 4 years ago · Juan Pablo Isaza 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!