【问题标题】:React Mobx - component not updating after store changeReact Mobx - 商店更改后组件不更新
【发布时间】:2016-11-20 09:17:30
【问题描述】:

使用 Mobx,更新商店后(即点击按钮)组件不会重新渲染。我已经安装了 mobx devtools,它在初始加载后没有显示任何内容,并且控制台中没有错误。任何想法我做错了什么?

Store.js:

import { observable } from 'mobx';

class Store {

    @observable me;

    constructor() {
        this.me = 'hello';
    }

    change_me(){
        this.me = 'test 1234';

    }

}


export default Store;

layout.js:

import React from "react";
import { observer } from 'mobx-react';


@observer
export default class Layout extends React.Component{

    render(){

        return(
            <div>
                <h1>{this.props.store.me}</h1>
              <button onClick={this.on_change}>Change</button>
            </div>
        )
    }

    on_change = () => {
        this.props.store.change_me();
    }
}

index.js:

import React from "react";
import ReactDOM from "react-dom";
import Layout from "./components/Layout";
import Store from "./Store";
import DevTools, { configureDevtool } from 'mobx-react-devtools';

// Any configurations are optional
configureDevtool({
    // Turn on logging changes button programmatically:
    logEnabled: true,
    // Turn off displaying conponents' updates button programmatically:
    updatesEnabled: false,
    // Log only changes of type `reaction`
    // (only affects top-level messages in console, not inside groups)
    logFilter: change => change.type === 'reaction',
});


const app = document.getElementById('app');
const store = new Store();

ReactDOM.render(

    <div>
        <Layout store={store} />
        <DevTools />
    </div>
, app);

【问题讨论】:

  • 我复制粘贴了您的代码,它可以在我的环境中使用。 after updating the store the component does not re-render 表示点击按钮后右键?
  • 没错。所以它对你有用吗?还能是什么?
  • 只是一个猜测,检查你的导入目录,是否正确。
  • 导入目录?那会使整个应用程序根本无法运行,不是吗?该应用程序正在运行,只需单击按钮后进行更新。
  • 你读过this吗?您可能需要将 transform-decorators-legacy 放在插件列表的首位。

标签: reactjs mobx mobx-react


【解决方案1】:

我首先将@action 添加到您的change_me() 函数中。据我了解,它并不总是完全必需的,但是我在自己的代码中遇到过几次这样的问题,但我忘记添加了。

另外按照@mweststrate 的建议发布您的 .babelrc,因为它可以帮助其他人检查是否加载了正确的插件。

【讨论】:

    【解决方案2】:

    只需添加 makeObservable(this);在如下构造函数中

        constructor() {
           makeObservable(this);
        }
    

    【讨论】:

      【解决方案3】:

      我的猜测是有未初始化的@observable。这是非常违反直觉的,但 Babel 不能很好地处理这些。即使添加 @observable me = undefined 可能会有所帮助(当你在那里分配东西时,请参阅生成的 js 代码。通常我会完全删除构造函数并将初始化移至声明(即 @observable me = "hello" 没有构造函数)。然后它应该可以正常工作。

      【讨论】:

        【解决方案4】:

        观察 this 上下文的绑定。

        <button onClick={this.on_change}>Change</button>
        

        this 引用不会指向该类,因此当您实际单击它时,它可能会说一些类似于 no props on undefined 的内容。改为:

          <button onClick={this.on_change.bind(this)}>Change</button>
        

        应该修复它。或者更好的是,在构造函数中绑定上下文,这样它就不会在每次渲染时重新绑定

         constructor(props) {
           super(props)
           this.on_change = this.on_change.bind(this)
         }
        

        然后你可以回到你的

        【讨论】:

        • 这是一个非常错误的答案,有两个原因。首先,on_change 绑定到 this,通过它的声明方式,所以 {this.on_change} 的原始调用是好的。然后,在 {} 中包含 .bind() 是一种反模式,会导致性能下降,并且永远不应该使用。
        • @Nopik 实际上并非如此。 on_change 未绑定到 this。 React 会自动绑定类似的方法,但前提是组件是通过 createClass 声明的,而不是在使用 ES6 类时。
        • @Nopik 正如苏丹所说,你真的需要阅读你的绑定。此外,正如我在下面演示的,正确的模式是在构造函数中绑定。你也可以在这里阅读更多内容:egorsmirnov.me/2015/08/16/react-and-es6-part3.html
        • on_change 已经绑定,因为它是这样写的:on_change = () =&gt; { ... } 在实例化过程中会导致绑定。有关更多信息,请参阅ES Public Class Fields 提案。
        • @dpastoor 1) Sulthan 是错误的,2) 您发布的链接列出了用作方法 4 的方法,作为绑定到此的有效方法(显然还没有在标准中OP 使用扩展)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-10
        • 2019-09-16
        • 1970-01-01
        • 2018-09-01
        • 2020-07-23
        • 2019-11-20
        • 1970-01-01
        相关资源
        最近更新 更多