反应;如何收集用户状态输入并在提交时附加到新组件
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;
&nbsp;&nbsp;&nbsp;&nbsp;所以我在这里有一个功能组件,并且在每个按钮时,我都会设置“值”的值。有效。不起作用的是在我的数组中获得多个值。我尝试了很多方法,比我想列出的更多...
&nbsp;&nbsp;&nbsp; nbsp;我想要一系列可以做的7件事要做,然后我将其作为道具将其传递给,并让该组件用新卡附加到DOM,并用新的卡表示TODO项目...直接输入提交后...
在Vanilla JS中,我简单地使用document.createlement('div')。innerhtml = &lt; p&gt; $ {input.Value} >(总结) 但是我无法弄清楚如何在反应中做到这一点……任何帮助,主要包括我在误解反应用法的地方都将是如此之好!
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;
So I have a functional component here, and I have useState setting the value of 'value' upon each button click. That works. What doesn't work is getting more than one value into my array. I have tried many ways, more than I want to list...
I want an array of let's say, 7 items of things to do, which I will then pass as props to and have THAT component append the DOM with a new card stating the todo items...directly after input submission...
In vanilla JS I have achieved this simply by using document.createElement('div').innerHTML = <p>${input.value}</p>
(summarized)
But I cannot figure out how to do this in react...Any help, mainly including where I am misunderstanding React usage, would be oh so great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当组件状态更改时,只有重新渲染才会发生。
它不使用局部变量(
让ARR = [];
),它应该是另一个反应状态钩子:然后您可以添加新的todos:如下:
Only re-render will happen when a component state changes.
Instead of using local variables (
let arr = [];
), it should be another react state hook like:Then you can add new todos as below:
您可以使用React状态来完成此类任务:
You can do such task by using React state: