无法对未安装的组件执行 React 状态更新。这是 no-op - Mob X 相关

Can't Perform a React State Update on an Unmounted Component. This is a no-op - Mob X Related

我目前正在开发一种智能家居类应用程序并且是初学者,我正在使用 mobX 设置活动房间名称。

我在“仪表板”组件中将它们设置为容器的标题。然后我想使用存储在里面的变量来过滤另一个名为“room”的组件中的房间。如果我 hard-code 房间名称它有效,但是当我用实际变量替换房间名称时它会抛出一堆错误。

我已经尝试过在 componentWillUnmount() 中声明一些东西,但到目前为止还没有奏效。

这是我设置变量的地方

handleClick(e){
      
        this.props.roomStore.room = e.target.closest('.btn__toggle').querySelector('.btn__headline').innerHTML;
      }

这是我要订房的地方

loadRooms(){
  if(this.state.isLoaded){
    var locations = this.state.things.filter( function (e){
      return e.location == this.props.roomStore.room}); //<- this is the relevant variable
          const habitems = locations.map((i, key) => 
          <div className="btn__toggle" key={i.UID} type="submit">
          <div className="btn__headline">{i.label}</div>
          </div>
              )
              return habitems;
            }         
}

这是我渲染我的项目的地方:

render() {
   
        if(this.state.isLoaded){
          return (
            <div>
          <h1 id="h1">{this.props.roomStore.room}</h1>
          <div className="btn__wrapper">{this.loadRooms()}</div>
          </div>
          );
        }

我认为它可能不起作用,因为在您实际打开正确的组件之前在另一个组件中设置了变量,在渲染中使用它,所以整个组件在安装之前就已经渲染了。

但我可能错了。任何帮助将非常感激。 发生的错误是:

index.module.js:860 Uncaught TypeError: Cannot read property 'props' of undefined

The above error occurred in the component

Uncaught (in promise) TypeError: Cannot read property 'props' of undefined

react-dom.development.js:506 Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

您遇到的问题是您的过滤器函数无法访问 this,从而导致错误 Cannot read property 'props' of undefined。您可以预先使用 destructuring assignment 从您的道具中过滤掉您的 roomStore 并使用它而不是访问 this.props.

loadRooms() {
    const { roomStore } = this.props;
    if (this.state.isLoaded) {
        var locations = this.state.things.filter( function (e){
            return e.location == roomStore.room
        });

        /* ... */
    }
}

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.