如果参数是对象或数组,为什么 useState() 会无限循环运行?

问题描述 投票:0回答:1

我在 Deno Fresh 中使用

useState()
钩子,它基于 Preact。在一个岛上我有:

import { useState } from "preact/hooks";

export default function(){
  const [state, setState] = useState('hi')
  const str = 'a'
  setState(str)
  console.log(state)
} 

这个效果很好。但如果我使用一个对象来代替:

- const str = 'a'
- setState(str)
+ const obj = {a: 'a'} 
+ setState(obj)

然后就陷入了无限循环。这是为什么?

reactjs react-hooks infinite-loop freshjs
1个回答
0
投票

这归结为一个问题:

console.log("a" === "a");
console.log({ x : "a" }  === { x : "a"}); //Why is this false when above is true

© www.soinside.com 2019 - 2024. All rights reserved.