Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react 中操作数组出现的问题 #55

Open
xiaochengzi6 opened this issue Oct 11, 2022 · 1 comment
Open

react 中操作数组出现的问题 #55

xiaochengzi6 opened this issue Oct 11, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@xiaochengzi6
Copy link
Owner

IDE在线版

import { useEffect, useState } from 'react';

export default function App() {
  const [instruct, setInstruct] = useState([0, 1, 2]);

  const increase = () => {
    console.log('lastArr', instruct);
    const length = instruct.length - 1;
    const _instruct = instruct.slice();
    const target = ++_instruct[length];
    console.log('现在的问题:', target);
    _instruct.push(target);
    setInstruct(_instruct);
    console.log('currentArr', _instruct);
  };

  return (
    <div>
      <button onClick={increase}>Click</button>
    </div>
  );
}

image
这里的 ++[0,1,2][2] 看样子会修改原数组的中2 索引的值然后在赋值给 target

@xiaochengzi6
Copy link
Owner Author

倒不是是react 中的问题 数组中原本就不能这样使用
image

@xiaochengzi6 xiaochengzi6 self-assigned this Oct 11, 2022
@xiaochengzi6 xiaochengzi6 added the bug Something isn't working label Oct 11, 2022
@xiaochengzi6 xiaochengzi6 reopened this Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant