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

282
Views
La entrada de React Js está perdiendo el foco cuando intento escribirlo, pero solo cuando se agrega onChange

El problema al que me enfrento es realmente molesto, cuando intento editar el valor predeterminado de mi texto de entrada, el campo de entrada no escribe mis cambios o pierde el foco solo después de escribir el primer carácter y vuelve a su valor predeterminado . Pero tenga en cuenta que esto solo sucede cuando agrego el evento onChange. Si lo elimino, la entrada funciona normalmente. Lo que está sucediendo

 const AddMaker = () => { const [make, setMake] = useState(); function ConditionalTableRow(props) { let { item, index } = props; if ( index === editModeIndex) return (<TableRowWithSave item={item} index={index} />) else return (<TableRowWithEdit item={item} index={index} />) } function TableRowWithSave(props) { let { item } = props; return ( <TableRow> <TableCell> <input type="text" defaultValue={item.make} onChange={e => setMake(e.target.value)} /> </TableCell> <TableCell> <button className="palletSaveButton" onClick={handleSaveClick}> Save </button> </TableCell> </TableRow> ) } return ( <TableBody> { records.map((item, index) => ( <ConditionalTableRow key={index} item={item} index={index} /> )) } </TableBody> ); }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Intente agregar un atributo de value al elemento de entrada.

 <input type="text" value={make} defaultValue={item.make} onChange={e => setMake(e.target.value)} />
about 4 years ago · Juan Pablo Isaza Report

0

Mueva useState a TableRowWithSave y use value en lugar de defaultValue

 function ConditionalTableRow(props) { let { item, index } = props; if ( index === editModeIndex) return (<TableRowWithSave item={item} index={index} />) else return (<TableRowWithEdit item={item} index={index} />) } function TableRowWithSave(props) { let { item } = props; const [make, setMake] = useState(item.make); return ( <TableRow> <TableCell> <input type="text" value={make} onChange={e => setMake(e.target.value)} /> </TableCell> <TableCell> <button className="palletSaveButton" onClick={handleSaveClick}> Save </button> </TableCell> </TableRow> ) } const AddMaker = () => { return ( <TableBody> { records.map((item, index) => ( <ConditionalTableRow key={index} item={item} index={index} /> )) } </TableBody> ); }

EDITAR: también es mejor mover ConditionalTableRow y TableRowWithSave fuera AddMaker

about 4 years ago · Juan Pablo Isaza Report

0

La respuesta corta es posible porque con cada pulsación de tecla estás provocando una nueva representación con el evento onChange.

Esta es una gran pregunta, y tuve el mismo problema que era de 3 partes.

  1. Claves generadas aleatoriamente.
  2. Tipo de evento incorrecto.
  3. Atributo JSX de reacción incorrecta.

Teclas : cuando usa teclas aleatorias, cada renderizado hace que reaccione para perder el foco (key={Math.random()*36.4621596072}) .

EventTypes : onChange provoca una nueva representación con cada pulsación de tecla, pero esto también puede causar problemas. onBlur es mejor porque se actualiza después de hacer clic fuera de la entrada. Una entrada, a menos que desee "bind" a algo en la pantalla (constructores visuales), debe usar el evento onBlur .

Atributos : JSX no es HTML y tiene sus propios atributos (className,...) . En lugar de usar el valor, es mejor usar defaultValue={foo} en una entrada.

Una vez que cambié estas 3 cosas, funcionó muy bien. Ejemplo a continuación.

Padre:

 const [near, setNear] = useState( "" ); const [location, setLocation] = useState( "" ); <ExperienceFormWhere slug={slug} questionWhere={question_where} setLocation={handleChangeSetLocation} locationState={location} setNear={setNear} nearState={near} key={36.4621596072}/>

Niño:

 <input defaultValue={locationState} className={slug+"_question_where_select search_a_location"} onBlur={event => setLocation(event.target.value)}/>

un gran recurso para la creación de formularios es: https://react-hook-form.com/

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!