简体   繁体   English

在 React 中单击按钮添加 1

[英]Add 1 with the click of a button in React

Now it's time for the next phase in my app development:现在是时候进入我的应用程序开发的下一阶段了:

function App() {
  const [number, setNumber] = useState('');
  const [totPoints, setTotPoints] = useState('');
  const [players, setPlayers] = useState(getLocalStorage());

const onOnePointMade = (e) => {
    e.preventDefault();
    const existingScorer = players.find((player) => player.nummer === number);

    if (existingScorer) {
      setTotPoints(+totPoints + 1);
      const newPlayer = {
        totPoints: totPoints,
      };
      setPlayers([...players, newPlayer]);
      console.log(totPoints);
    } else {
      setTotPoints(+totPoints + 1);
      const newPlayer = {
        id: nanoid(4),
        nummer: number,
        totPoints: totPoints,
      };
      setPlayers([...players, newPlayer]);
    }
    setNumber('');
  };

with the above code, I want in pure English:使用上面的代码,我想要纯英语:

  1. check if the player (= number ) is on the list players检查玩家 (= number ) 是否在players列表中
  2. if yes: add 1 (+ 1) to his points scored so far ( totPoints )如果是:将 1 (+ 1) 加到他目前得分 ( totPoints )
  3. if no: add number and one point to the list (totPoints = 1)如果否:将数字和一个点添加到列表中(totPoints = 1)

Grateful for help with this problem as well也感谢帮助解决这个问题

Thanks in advance提前致谢

PS I tried with the code I got from @Robin PS 我尝试使用从@Robin 获得的代码

const onOnePointMade = (e) => {
    e.preventDefault();
    const scorer = [...players];
    const existingScorer = scorer.find((player) => player.number === number);
    const new_totpoints = totPoints + 1;

    if (existingScorer) {
      setTotPoints(new_totpoints);
      existingScorer.totPoints = new_totpoints;
      setPlayers(scorer);
    } else {
      const newScorer = {
        id: nanoid(4),
        number: number,
        totPoints: new_totpoints,
      };
      setPlayers([...players, newScorer]);
    }
  };

Unfortunately, it did not work quite as I intended: the first time I add player number 3 it will be correct ie 1 point if player number 3 scores more points is added correctly.不幸的是,它并没有像我预期的那样工作:我第一次添加 3 号球员时它是正确的,即如果 3 号球员得分更多,则正确添加 1 分。 BUT, Let's say he has scored 3 points, if I now add player number 4, it is listed that he has scored 4 points, instead of 1 point I hope you understand: i attach a link: https://github.com/peter-swe/tot-points但是,假设他得了3分,如果我现在加上4号球员,列出的是他得了4分,而不是1分希望你理解:我附上一个链接: https://github.com/ peter-swe/tot-points

In your code for the existing player block, you are trying to add a new player.在现有播放器块的代码中,您正在尝试添加新播放器。 By looking at your explanation it should be finding the player and update their totpoints .通过查看您的解释,它应该找到玩家并更新他们的totpoints

We can't update the state's property directly so take a copy of the state, then update the totpoint in the new copy, and set the new copy to the state.我们不能直接更新 state 的属性,所以复制 state,然后更新新副本中的totpoint ,并将新副本设置为 state。

    const _players = [...players];
    const existingScorer = _players.find((player) => player.nummer === number);
    const new_totpoints = totPoints + 1
    if (existingScorer) {
      setTotPoints(new_totpoints);
      existingScorer.totPoints = new_totpoints
      setPlayers(_players);
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM