首先组件接收的props值 ,它是一个可读的值 ,不允许修改的,所以我们不能直接修改props
看一下下面这张图,这是基于ant.design Tree组件生成的权限设置界面,编辑的时候,需要知道当前角色的权限,然后还需要新增或删减权限(需要改变数据),所以初始的时候就需要把props映射到state了


getDerivedStateFromProps是一个静态函数,它不能使用this,可以用来代替componentWillReceiveProps
state变化, 每次render的时候都会调用getDerivedStateFromProps,所以使用getDerivedStateFromProps时要谨慎
具体参考代码
class Update extends Component {state = {// 选择框选中,会提交生效checkedKeys: [],// 单击文案选中,不提交selectedKeys: [],};static getDerivedStateFromProps(nextProps, prevState) {// current为传为的数据,编辑时才会有const { current } = nextProps;const state = prevState;// show也是编辑的时候才会有,展示modal弹窗// checkedKeys为Tree的勾选数据if (nextProps.show) {if (!state.checkedKeys.length) {if (current) {// auth为初始的权限列表if (current.auth && current.auth.length) {state.checkedKeys = current.auth;return { ...state };}}}} else {// 关闭弹窗时,清空Tree的选择数据state.checkedKeys = [];return { ...state };}// 不改变state时,返回null,这个必须有return null;}render() {// ...省略<TreecheckableonCheck={this.onCheck}checkedKeys={this.state.checkedKeys}onSelect={this.onSelect}selectedKeys={this.state.selectedKeys}defaultExpandAll>{this.renderTreeNodes(menuList)}</Tree>// ...省略}
https://reactjs.org/docs/react-component.html#static-getderivedstatefromprops
ant.design 4.x获取form表单的方式
ant.design v5 and 中如何在class中使用umijs的useModel调用initialState
react之ref提示:Using string literals in ref attributes is deprecated,不能使用字符串,怎么处理?
react不能在 render里设置state,也不能在componentWillReceiveProps里设置state,会导致性能问题。如果要通过属性改变state,怎么做呢?可以利用getDerivedStateFromProps生命周期函数
Next.js 是一个轻量级的 React 服务端渲染应用框架。
通过箭头函数或bind方式,对绑定事件传参
React Hot Loader是一个插件,允许React组件在不丢失状态的情况下进行实时重新加载。它适用于Webpack和其他支持热模块替换(HMR)和Babel插件的捆绑器
Warning: `getFieldDecorator` will override `value`, so please don't set `value` directly and use `setFieldsValue` to set it.
React 的核心思想是:封装组件。 各个组件维护自己的状态和 UI,当状态变更,自动重新渲染整个组件。 基于这种方式的一个直观感受就是我们不再需要不厌其烦地来回查找某个 DOM 元素,然后操作 DOM 去更改 UI