dlrandy / note-issues

2 stars 0 forks source link

React Best Practices 06 #33

Open dlrandy opened 6 years ago

dlrandy commented 6 years ago

React 里的反模式 使用props初始化state:但是在props不变的情况下还是会使用它。它会有俩个问题:1、重复数据源(props, state)2、props改变,state不会更新。当然这两个问题都可以解决。

直接修改state:会导致组件的一致性和性能问题。因为1、state改变,但是组件没有重新渲染2、后续的setState会使用改变过的state。记住state是要存储 哪些需要在render方法里使用的值 。使用PureComponent的时候,直接改变state对其有一个负面影响不会rerender。

把index作为key:key必须是唯一的稳定的。React要使用它来判断是否是新的或者当props或者state变化时是否必须更新。产生性能问题

不要在DOM 元素上spread props: 可以增加不认识的attr给html,解决的办法是使用一个专门DOm 使用的props例如domProps

dlrandy commented 6 years ago

贡献React 发现bug的时候,使用reactjs/69z2wepo的jsfiddle来浮现问题,确保是bug 不是app code问题 react的人员快速理解问题。fix之后所有的test green,就可以提pr

发布npm包: npm init -y npm adduser npm publish

npm version [patch, minor, major] npm publish

React story book有助于创建在线样式指导,方便作者维护,方便用户试用