如何在 componentWillMount 事件中重构代码?

这里的啤酒

由于弃用,我正在重构我的 reactjs (react v16.0.0) 组件并尝试将 componentWillMount 事件的名称更改为 componentDidMount。这是原始代码:

componentWillMount() {
      this.setState({
        myVar: this.getValue(),
      });
    }
}

当我将其更改为 componentDidMount 时,出现 eslint 错误:

Do not use setState in componentDidMount

我怎样才能解决这个问题?如何修复这个 eslint 错误?

尤利奥·阿莱曼·希门尼斯

您可以尝试设置状态而不是直接在 componentDidMount

componentDidMount() {
    this.onMount(this.getValue());
}

onMount(newValue){
    this.setState({
        myVar: newValue
    });
}

你也可以禁用你的eslint插件

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在reactjs componentWillMount()中,如何获取哪个组件?

来自分类Dev

如何使用 react native 映射 componentWillMount 中的嵌套数组

来自分类Dev

如何在Haskell中重构此代码

来自分类Dev

如何在Haskell中重构此代码

来自分类Dev

状态未在componentWillMount中更新

来自分类Dev

使用Redux时如何在componentWillMount中加载数据

来自分类Dev

如何在Gradle Ant任务中重构通用代码?

来自分类Dev

如何在调用 super 的代码中重构重复性

来自分类Dev

如何在 Kotlin 中重构这个 Android TakePicture 代码?

来自分类Dev

如何在 React 中重构 switch 语句的代码?

来自分类Dev

如何使用堆栈强制调用 componentWillMount 方法?

来自分类Dev

React.js中componentWillMount调用的顺序

来自分类Dev

找不到在componentWillMount中设置的属性

来自分类Dev

在 componentWillMount 中链接 api - 异步等待或承诺

来自分类Dev

redux如何使用react-router在componentWillMount上重定向

来自分类Dev

如何使用UNSAFE componentWillMount方法找出哪个软件包?

来自分类Dev

React 未能在 componentWillMount() 中调用的函数中设置状态

来自分类Dev

在Scala中,如何使用Option类重构代码?

来自分类Dev

在Scala中,如何使用Option类重构代码?

来自分类Dev

如何重构此代码以从数组中获取 Mongo 数据?

来自分类Dev

如何在代码中为DataTemplate分配事件处理程序?

来自分类常见问题

ReactJS中的componentWillMount和componentDidMount有什么区别?

来自分类Dev

componentDidMount或componentWillMount在React-Native中不起作用?

来自分类Dev

如何在Java中重构此HashSet

来自分类Dev

如何在Yii中重构AJAX流程

来自分类Dev

如何在php中重构增长的类?

来自分类Dev

如何在 JSX 中重构 switch 语句

来自分类Dev

ComponentDidMount与ComponentWillMount?

来自分类Dev

我们如何在Meteor中从模板的事件代码中调用另一个模板的事件代码?

Related 相关文章

  1. 1

    在reactjs componentWillMount()中,如何获取哪个组件?

  2. 2

    如何使用 react native 映射 componentWillMount 中的嵌套数组

  3. 3

    如何在Haskell中重构此代码

  4. 4

    如何在Haskell中重构此代码

  5. 5

    状态未在componentWillMount中更新

  6. 6

    使用Redux时如何在componentWillMount中加载数据

  7. 7

    如何在Gradle Ant任务中重构通用代码?

  8. 8

    如何在调用 super 的代码中重构重复性

  9. 9

    如何在 Kotlin 中重构这个 Android TakePicture 代码?

  10. 10

    如何在 React 中重构 switch 语句的代码?

  11. 11

    如何使用堆栈强制调用 componentWillMount 方法?

  12. 12

    React.js中componentWillMount调用的顺序

  13. 13

    找不到在componentWillMount中设置的属性

  14. 14

    在 componentWillMount 中链接 api - 异步等待或承诺

  15. 15

    redux如何使用react-router在componentWillMount上重定向

  16. 16

    如何使用UNSAFE componentWillMount方法找出哪个软件包?

  17. 17

    React 未能在 componentWillMount() 中调用的函数中设置状态

  18. 18

    在Scala中,如何使用Option类重构代码?

  19. 19

    在Scala中,如何使用Option类重构代码?

  20. 20

    如何重构此代码以从数组中获取 Mongo 数据?

  21. 21

    如何在代码中为DataTemplate分配事件处理程序?

  22. 22

    ReactJS中的componentWillMount和componentDidMount有什么区别?

  23. 23

    componentDidMount或componentWillMount在React-Native中不起作用?

  24. 24

    如何在Java中重构此HashSet

  25. 25

    如何在Yii中重构AJAX流程

  26. 26

    如何在php中重构增长的类?

  27. 27

    如何在 JSX 中重构 switch 语句

  28. 28

    ComponentDidMount与ComponentWillMount?

  29. 29

    我们如何在Meteor中从模板的事件代码中调用另一个模板的事件代码?

热门标签

归档