简体   繁体   English

React Hooks - 在 onClick 事件期间拼接更新状态有问题

[英]React Hooks - having trouble with splice updating state during onClick event

SANDBOX DEMO 沙盒演示

Here is the snippet of my code that's running.这是我正在运行的代码片段。 When I console.log it looks fine, but for some reason it won't set the state.当我 console.log 看起来不错,但由于某种原因它不会设置状态。 When clicking a box it should delete by using splice.当单击一个框时,它应该使用 splice 删除。 I know there's a basic reason why it's failing but I can't seem to grasp it.我知道它失败有一个基本原因,但我似乎无法理解。

  const handleClick = (index) => {
    const newBoxOrder = order;
    newBoxOrder.splice(index, 1);
    const newState = newBoxOrder;
    console.log(newState);
    setOrder(newState);
  };

CodeSandbox 代码沙盒

 const handleClick = (index) => {
        const newBoxOrder = order;
        newBoxOrder.splice(index, 1);
        const newState = newBoxOrder;
        console.log(newState);
        setOrder([...newState]); 
      };

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

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