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

118
Views
¿Cómo establecer el valor inicial de useState con el valor de otro estado establecido en un gancho useEffect?
  1. Obtengo datos de usuario de Firestore en un gancho useEffect y luego pongo los datos en un estado. Esta parte funciona bien.
 const [profile, setProfile] = useState("") useEffect(() => { // Get user profile data from firestore.. setProfile(userData) }, [])
  1. Tengo entradas de radio HTML donde el usuario puede cambiar su género, pero necesito establecer el género inicial (uno que elijan durante el registro) con un estado.
 const defaultGender = profile.gender const [gender, setGender] = useState(defaultGender) <input onChange={e => setGender(e.target.value)} type="radio" id="Man" name="gender" value="Man" checked={defaultGender === "Man"} /> <label htmlFor="Man">Man</label> <input onChange={e => setGender(e.target.value)} type="radio" id="Woman" name="gender" value="Woman" checked={defaultGender === "Woman"} /> <label htmlFor="Woman">Woman</label>

El problema es que, con este código, console.log(gender) devuelve undefined y no puedo verificar (seleccionar) un género diferente ya que está atascado en Hombre.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Parece que solo necesitas:

  1. Use el gender del estado (no defaultGender ) para la propiedad de la entrada checked .
  2. Actualice su estado de gender después de la actualización del estado del profile .

CódigoPen

 const { useState, useEffect, Fragment } = React; const Form = () => { const [profile, setProfile] = useState({}); const [gender, setGender] = useState(profile.gender); useEffect(() => { // Assuming you have an async request here... const profile = { gender: "Woman" }; setProfile(profile); setGender(profile.gender); }, []); return ( <Fragment> <input onChange={(e) => setGender(e.target.value)} type="radio" id="Man" name="gender" value="Man" checked={gender === "Man"} /> <label htmlFor="Man">Man</label> <input onChange={(e) => setGender(e.target.value)} type="radio" id="Woman" name="gender" value="Woman" checked={gender === "Woman"} /> <label htmlFor="Woman">Woman</label> </Fragment> ); }; ReactDOM.render(<Form />, document.body);
 <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>

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!