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

296
Views
Reaccionar; cómo recopilar la entrada del estado del usuario y agregar al nuevo componente al enviar
import './App.css'; import GoalBox from './Components/GoalBox'; import { useState, useEffect, useRef } from 'react'; function App() { const [value, setValue] = useState('') const inputReset = useRef(null) let arr = []; const submitValue = () => { const todoList = { 'todo': value } console.log(todoList); inputReset.current.value = ''; // resets input field arr.push(todoList) console.log('todo array', arr) } return ( <div className="App"> <h1>List of things to do</h1> <input ref={inputReset} onChange={(e) => setValue(e.target.value)} /> <button onClick={submitValue}>Add New To do</button> </div> ); } export default App;

Así que tengo un componente funcional aquí, y tengo useState configurando el valor de 'valor' en cada clic de botón. Eso funciona. Lo que no funciona es obtener más de un valor en mi matriz. Lo he intentado de muchas maneras, más de las que quiero enumerar...
Quiero una serie de, digamos, 7 elementos de cosas que hacer, que luego pasaré como accesorios y haré que ESE componente agregue el DOM con una nueva tarjeta que indique los elementos pendientes... directamente después del envío de entrada...
En Vanilla JS lo logré simplemente usando document.createElement('div').innerHTML = <p>${input.value}</p> (resumido) Pero no puedo entender cómo hacer esto en reaccionar. .Cualquier ayuda, incluyendo principalmente donde estoy malinterpretando el uso de React, ¡sería genial!

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

0

Solo se volverá a renderizar cuando cambie el estado de un componente.

En lugar de usar variables locales ( let arr = []; ), debería ser otro enlace de estado de reacción como:

 const [arr, setArr] = useState([]);

Luego puede agregar nuevos todos como se muestra a continuación:

 setArr((prevArr) => [...prevArr, todoItem]); 

 function App() { const [value, setValue] = React.useState(""); const inputReset = React.useRef(null); const [arr, setArr] = React.useState([]); const submitValue = () => { const todoItem = { todo: value }; setArr((prevArr) => [...prevArr, todoItem]); inputReset.current.value = ""; // resets input field }; return ( <div className="App"> <h1>List of things to do</h1> <input ref={inputReset} onChange={(e) => setValue(e.target.value)} /> <button onClick={submitValue}>Add New To do</button> {arr.map(({ todo }) => ( <div key={todo}>{todo}</div> ))} </div> ); } ReactDOM.render(<App />, document.querySelector('.react'));
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div class='react'></div>

about 4 years ago · Juan Pablo Isaza Report

0

Puede realizar dicha tarea utilizando el estado React:

 import './App.css'; import { useState } from 'react'; function App() { const [value, setValue] = useState('') const [arr, setArr] = useState([]); const submitValue = () => { setValue(""); // resets input field setArr([...arr, { todo: value }]); // Use shallow copy to give a new ref } return ( <div className="App"> <h1>List of things to do</h1> <input value={value} onChange={(e) => setValue(e.target.value)} /> <button onClick={submitValue}>Add New To do</button> {arr.map(({todo}) => <p key={todo}>{todo}</p>)} </div> ); } export default App;
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!