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

163
Views
Reaccionar: hacer clic en un enlace de enrutador antes de que el enlace obtenga un valor

el problema es que el cambio de URL ocurre antes de que el enlace obtenga un valor. Recibo el estado directamente a través del componente, pero antes de que eso suceda, ya se hizo clic en el componente. el valor se asigna después del cambio de URL, por lo que devuelve indefinido. ¿Cómo arreglaría esto?

 import React, {useEffect, useState} from "react"; import "./Main.scss"; import CategoryWindow from "./CategoryWindow" import RecipeSelection from "./RecipeSelection"; import RecipeWindow from "./RecipeWindow"; import {BrowserRouter as Router, Switch, Route, Link} from "react-router-dom"; const Main =()=> { const [showElement, setShowElement] = useState("category"); const [selectedCategory, setSelectedCategory] = useState(); useEffect(()=> { if (selectedCategory) { setShowElement("recipeSelection") } }); return ( <> <main className="main"> <Router> <div> <div> {showElement === "category" ? <Link to={`/${selectedCategory}`}> <CategoryWindow passSelectedCategory={setSelectedCategory} /> </Link> : ''} </div> </div> </Router> </main> </> ) } export default Main;
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

intente cambiar useEffect a esto para que no se vuelva a procesar cada milisegundo y ¿puede mostrar cómo se ve CategoryWindow?

 useEffect(()=> { if (selectedCategory) { setShowElement("recipeSelection") } }, [selectedCategory]);

No creo que un enlace deba colocarse directamente en un enrutador, pero si desea obtener la categoría del enlace en el componente CategoryWindow, puede obtenerla directamente del enlace sin pasar el estado seleccionado Categoría

Principal.js

 <> <Router> <Route path="item"> <Route path=":category" element={<CategoryWindow />}/>} </Route> <Route path="/" element={<Component />}/>} </Router> </>

CategoryWindow.js

 import { useParams} from 'react-router-dom'; const { category} = useParams() <> <div>{category}</div> // this is the actual selectedCategory </>

Componente.js

 const [selectedCategory, setSelectedCategory] = useState(); <> <Link to={"/category/" + selectedCategory}/> </>
about 4 years ago · Santiago Trujillo Report

0

pase [selectedCategory] como segundo argumento en useEffect.

 useEffect(()=> { if (selectedCategory) { setShowElement("recipeSelection") } },[selectedCategory]);

mira esto: Usando el Gancho de Efecto

about 4 years ago · Santiago Trujillo 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!