我在一个网球应用程序中工作,我在管理许多州时遇到了问题.我会解释:网球锦标赛第一轮有128名这样的球员,每一行都有球员和种子选手.

enter image description here enter image description here

Array.from

const oddIndex = index * 2 + 1
  const evenIndex = index * 2 + 2

  const getCurrentSeed = (position) => {
    const seed = !objectIsEmpty(games) && games?.rounds[round.round]?.positions[position]?.seed

    return seed || ''
  }

  const getCurrentATPPlayer = (position) => {
    const ATPPlayer = !objectIsEmpty(games) && games?.rounds[round.round]?.positions[position]?.atp_player
    return ATPPlayer || {}
  }

<div className="player">
 <ATPPlayerFinderWrapper position={oddIndex} setATPPlayerId={setATPPlayerId} setCurrentPosition={setCurrentPosition} ATPPlayer={getCurrentATPPlayer(oddIndex)} />
</div>
<div className="seed">
    <TextField
      id={`seed${oddIndex}`}
      label="Seed"
      variant="standard"
      onChange={(e) => setSeed(e.target.value)}
      InputProps={{ disableUnderline: true }}
      value={getCurrentSeed(oddIndex)}
   />
</div>

});

我的大问题是,当我从后面获得行时,我需要每个文本字段的状态,但它不起作用,可能会很多.

推荐答案

您可以做的是,让数组处于一种状态,并将其每个值设置为''.

const [seeds, setSeeds] = useState(Array(128).fill(''));
const [ATPPlayers, setATPPlayers] = useState(Array(128).fill({}));

然后,只要状态改变,就设置它的值

const handleSeedChange = (position, value) => {
    const newSeeds = [...seeds];
    newSeeds[position] = value;
    setSeeds(newSeeds);
  };

  const handleATPPlayerChange = (position, player) => {
    const newATPPlayers = [...ATPPlayers];
    newATPPlayers[position] = player;
    setATPPlayers(newATPPlayers);
  };

在这里,它是根据您的问题完整的代码.

import React, { useState } from 'react';

const TennisBracket = ({ games, round }) => {
  const [seeds, setSeeds] = useState(Array(128).fill(''));
  const [ATPPlayers, setATPPlayers] = useState(Array(128).fill({}));

  const getCurrentSeed = (position) => seeds[position] || '';

  const getCurrentATPPlayer = (position) => ATPPlayers[position] || {};

  const handleSeedChange = (position, value) => {
    const newSeeds = [...seeds];
    newSeeds[position] = value;
    setSeeds(newSeeds);
  };

  const handleATPPlayerChange = (position, player) => {
    const newATPPlayers = [...ATPPlayers];
    newATPPlayers[position] = player;
    setATPPlayers(newATPPlayers);
  };

  return (
    <div>
      {Array.from(Array(128)).map((p, index) => {
        const oddIndex = index * 2 + 1;

        return (
          <div key={index}>
            <div className="player">
              {/* Use your ATPPlayerFinderWrapper or any other component */}
              {/* Assuming it has a callback to pass the selected player to the parent */}
              <ATPPlayerFinderWrapper
                position={oddIndex}
                setATPPlayerId={setATPPlayerId}
                setCurrentPosition={setCurrentPosition}
                ATPPlayer={getCurrentATPPlayer(oddIndex)}
                onPlayerChange={(player) => handleATPPlayerChange(oddIndex, player)}
              />
            </div>
            <div className="seed">
              <TextField
                id={`seed${oddIndex}`}
                label="Seed"
                variant="standard"
                onChange={(e) => handleSeedChange(oddIndex, e.target.value)}
                InputProps={{ disableUnderline: true }}
                value={getCurrentSeed(oddIndex)}
              />
            </div>
          </div>
        );
      })}
    </div>
  );
};

Reactjs相关问答推荐

在react中向数组状态添加值时出错

为什么安装FLOBIT后,所有的输入FIELD现在都有一个蓝色的轮廓?

For循环中的元素在Reaction中丢失挂钩

在Next.js中实现动态更改的服务器组件

后端关闭时如何在REACTION AXIOS拦截器中模拟数据?

在 React 中,如何在指向同一组件的路由中拥有未定义数量的多个动态路径段?

列表中的每个子项都应该有一个唯一的keyprops .在react 应用程序中

如何根据ReactJS中元素列表中的布尔值仅显示一次值

在react的useEffect钩子中,我的函数被调用,但只有第一个函数能够改变状态,第二个函数无法改变状态.

React:如何使用条件渲染和react 挂钩来更新另一个组件的状态?

React 测试库 - 如何断言异步函数之间的中间状态?

AWS 全栈应用程序部署教程构建失败

我正在try 使用 cypress 测试我的下拉列表,它 Select 值但不提交它们(手动工作)

在 Remix 中使用 chartjs 和 react-chartjs-2 会出现错误react-chartjs-2未在您的 node_modules 中找到

如何在 JSX 中使用 -webkit- 前缀?

MUI 卡组件上的文本未正确对齐

网格项不缩小以适应包含的可滚动列表

哪个 PrivateRouter 实现更好:高阶组件还是替换?

你如何使用 refs 访问 React 中映射子项的值?

如何在 React x 中返回组件?