输入状态的最佳实践 - 反应
我正在处理具有多个输入的组件。每个列表项目都是“匹配”,每场比赛有2个输入,您可以存储两个团队的目标。该输入是从地图中渲染的:
return (
<div>
<ul>
{groupA &&
groupA.map((match) => (
<li key={match.id} className="matches">
<img
className="home-img"
src={`${
teams.teams.filter((team) => team.id === match.homeTeam.id)[0]
.crestUrl
}`}
/>
{match.homeTeam.name}
<input
onChange={(e) =>
setGoalHome({
match: match.id,
home: match.homeTeam.name,
goalHome: e.target.value,
})
}
/>
vs
{match.awayTeam.name}
<input onChange={(e) => setGoalAway(e.target.value)} />
<img
className="away-img"
src={`${
teams.teams.filter((team) => team.id === match.awayTeam.id)[0]
.crestUrl
}`}
/>
</li>
))}
</ul>
<button onClick={handleSubmit}>Send</button>
</div>
);
问题是:处理输入状态的最佳实践是什么? Should I have to create a state for every input? On the submission, I want to send the data to a DB.
I´m dealing with a component which has several inputs. Each list item is a "match" and has 2 inputs per match which you can store the goals of the two teams. This inputs are rendered from a map:
return (
<div>
<ul>
{groupA &&
groupA.map((match) => (
<li key={match.id} className="matches">
<img
className="home-img"
src={`${
teams.teams.filter((team) => team.id === match.homeTeam.id)[0]
.crestUrl
}`}
/>
{match.homeTeam.name}
<input
onChange={(e) =>
setGoalHome({
match: match.id,
home: match.homeTeam.name,
goalHome: e.target.value,
})
}
/>
vs
{match.awayTeam.name}
<input onChange={(e) => setGoalAway(e.target.value)} />
<img
className="away-img"
src={`${
teams.teams.filter((team) => team.id === match.awayTeam.id)[0]
.crestUrl
}`}
/>
</li>
))}
</ul>
<button onClick={handleSubmit}>Send</button>
</div>
);
The question is: what would be the best practice to handle the state of the inputs? Should I have to create a state for every input? On the submission, I want to send the data to a DB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议为所有这些创建一个全球状态经理,因为您的输入是动态生成的,并希望将数据发送到您的数据库
时,这将是一件好事
当数据准备发送我的意思是这样的东西
编辑:更改代码以使这个想法有些清楚,
我没有顺便测试它。
I would recommend creating a global state manager for all of them since your input are dynamically generated and want to send data to your database
it would be good when data ready to send
What I mean is something like this
Edit: changed the code to make the idea a little clear
I didn't test it by the way .