【问题标题】:Why does calling setState in ComponentDidMount cause error?为什么在 ComponentDidMount 中调用 setState 会导致错误?
【发布时间】:2017-09-29 08:01:05
【问题描述】:

我假设包react-virtual-list 在使用VirtualList 时调用setState,但这不应该导致componentDidMount 内部出现问题,对吧?

Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the vlist component.

componentDidMount = () => { this.MyVirtualList = VirtualList({container: this._itemsList})(this.MyList) }

【问题讨论】:

  • 你能贴出组成你的组件的代码吗?
  • 什么是this.MyVirtualList?那是一个功能吗?请显示更多代码。

标签: javascript reactjs


【解决方案1】:

您正在将一个组件“初始化”为一个变量,但您并没有安装它。

要挂载一个组件,该组件必须在render方法中,否则该组件将不会被挂载到DOM中。

你为什么要这样做?也许,您应该渲染组件,然后使其可见或不可见。这是最简单的方法。

再见

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 2012-05-04
    • 2020-07-16
    • 2014-06-05
    相关资源
    最近更新 更多