Why is useState not triggering re-render?

后端 未结 3 1982
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 08:54

I\'ve initialized a state that is an array, and when I update it my component does not re-render. Here is a minimal proof-of-concept:

function App() {
  const         


        
3条回答
  •  再見小時候
    2020-12-06 09:29

    You need to copy numbers like so let old = [...numbers];

    useState doesn't update the value only if it has changed so if it was 44 and it became 7 it will update. but how can it know if an array or object have changed. it's by reference so when you do let old = numbers you are just passing a reference and not creating a new one

提交回复
热议问题