State Structure(V3)

2024-02-24  本文已影响0人  玫瑰的lover

一 ,需要避免的几种情况

关联的state

两个state具有关联性,定义成对象即可

矛盾的state

const isSending = status === 'sending' 
const isSent = status === 'sent'

冗余的state

const [count, setCount] = useState(**countProp**)

重复的state

在方法 a 和 b 中都能对某一个state进行更改,比如 Select 的 Option 绑定一个String 类型,而不是 Object 类型
bad case
good case

深度嵌套的state

The state should be flat (also known as "normalized")

二, Immer

通过use-immer库撰写更加简洁的代码,类似于ahooksuseReactive可以简化下面这段代码

setPerson({
  ...person, // Copy other fields
  artwork: { // but replace the artwork
    ...person.artwork, // with the same one
    city: 'New Delhi' // but in New Delhi!
  }
});

三,xxxList

上一篇 下一篇

猜你喜欢

热点阅读