输入状态的最佳实践 - 反应

发布于 2025-01-23 03:27:06 字数 1453 浏览 0 评论 0原文

我正在处理具有多个输入的组件。每个列表项目都是“匹配”,每场比赛有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2025-01-30 03:27:06

我建议为所有这些创建一个全球状态经理,因为您的输入是动态生成的,并希望将数据发送到您的数据库
时,这将是一件好事

当数据准备发送我的意思是这样的东西
编辑:更改代码以使这个想法有些清楚,

我没有顺便测试它。

   const [data,setData] = useState([])
   useEffect(()=>{
 if(groupA)
    groupA.ForEach(e=>data.push({matchId:e.match.id,goalAway:0,goalHome:0})
setData(data)
  }
},[groupA])
   function setGoalHome({matchId,goalHome}){
      let index = 0;
      const currentMatch = data.find((element,i) => {
           index = i
           return (element.matchId = matchId)
       }
      currentMatch.goalHome = goalHome
      data[index] = currentMatch
      setData(data)
   }
   function setGoalAway({matchId,goalAway}){
     let index = 0;
      const currentMatch = data.find((element,i) => {
           index = i
           return (element.matchId = matchId)
       }
      currentMatch.goalAway= goalAway
      data[index] = currentMatch
      setData(data)

   }
   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,
                    goalHome: e.target.value,
                  })
                }
              />
              vs
              {match.awayTeam.name}
              <input onChange={(e) => setGoalAway({
                    match: match.id,
                    goalAway: 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>
  );

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 .

   const [data,setData] = useState([])
   useEffect(()=>{
 if(groupA)
    groupA.ForEach(e=>data.push({matchId:e.match.id,goalAway:0,goalHome:0})
setData(data)
  }
},[groupA])
   function setGoalHome({matchId,goalHome}){
      let index = 0;
      const currentMatch = data.find((element,i) => {
           index = i
           return (element.matchId = matchId)
       }
      currentMatch.goalHome = goalHome
      data[index] = currentMatch
      setData(data)
   }
   function setGoalAway({matchId,goalAway}){
     let index = 0;
      const currentMatch = data.find((element,i) => {
           index = i
           return (element.matchId = matchId)
       }
      currentMatch.goalAway= goalAway
      data[index] = currentMatch
      setData(data)

   }
   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,
                    goalHome: e.target.value,
                  })
                }
              />
              vs
              {match.awayTeam.name}
              <input onChange={(e) => setGoalAway({
                    match: match.id,
                    goalAway: 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>
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文