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

101
Views
¿Cómo agregar una variable en el atributo de estilo jsx cuando ya hay un objeto?

Estoy tratando de alinear el texto en la etiqueta del área de texto escribiendo la lógica con el nombre de la variable myStyle , por lo que la lógica funciona, pero luego hice un cambio para el modo oscuro y agregué más estilo como objeto . Entonces myStyle deja de funcionar. Entonces, la pregunta es cómo agregar la variable myStyle en el atributo de estilo cuando ya existe un objeto dado, ¿cuál es el formato correcto?

 export default function TextForm(prop) { const [text, setText] = useState("Enter text here"); const [myStyle, setMyStyle] = useState({ textAlign:'left' }) const textAlign = ()=>{ if(myStyle.textAlign === 'left'){ setMyStyle({ textAlign: 'center' }) } else if(myStyle.textAlign === 'center'){ setMyStyle({ textAlign:'right' }) } else{ setMyStyle({ textAlign:'left' }) } } return ( <> <div className='container' style={{color: prop.mode==='light'?'Black':'white'}}> <h2>{prop.heading} </h2> <div className="mb-3"> <textarea className="form-control " style={myStyle,{ backgroundColor:prop.mode==='light'?'white':'grey', color:prop.mode==='light'?'black':'white'}} value={text} onChange= {handleOnChange} id="myBox" rows="6"></textarea> </div> <button className="btn btn-primary mx-1" onClick={textAlign} >Change Text Align</button> </div> </> ) }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Distribuya el objeto existente en uno nuevo, y en ese nuevo, enumere las propiedades adicionales deseadas.

 <textarea className="form-control " style={{ ...myStyle, backgroundColor:prop.mode === 'light' ? 'white' : 'grey', color: prop.mode === 'light' ? 'black' : 'white' }} value={text} onChange={handleOnChange} id="myBox" rows="6" ></textarea>

Aunque sería más elegante alternar una clase CSS en el elemento y hacerlo con reglas CSS en lugar de mediante propiedades establecidas a través de JS.

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!