【问题标题】:Mobx component not re-rendering on change in observable valueMobx 组件不会在可观察值发生变化时重新渲染
【发布时间】:2018-05-28 11:45:59
【问题描述】:

我正在创建一个 React 应用程序并使用 Mobx 进行状态管理。这是我的代码:

商店:

import { observable, action } from 'mobx';

class UnlockApp {

    @observable unlockPoints = 9;
    @observable pattern = null;
    @observable patternInProgress = false;
    @observable lastPoint = null;
    @observable allNodes = null;
    @observable lastNode = null;
    @observable currentX = null;
    @observable currentY = null;
    @observable isPatternLineEmpty = true;

    @action onPointPress = (point) => {
        this.pattern = [];
        this.pattern.push(point);
        this.patternInProgress = true;
        this.lastPoint = point;
        this.isPatternLineEmpty = false;
    }

    @action drawLine = (currentX, currentY) => {
        if (this.patternInProgress) {
            this.lastNode = this.allNodes[this.lastPoint].current;
            this.currentX = currentX;
            this.currentY = currentY;
        }
    }

    @action handleMouseEnter = (point) => {
        if (this.patternInProgress) {
            this.pattern.push(point);
            this.lastPoint = point;
            this.isPatternLineEmpty = false;
        }
    }

    @action stopCounting = () => {
        this.patternInProgress = false;
        this.lastPoint = null;
    }

    @action updateNodes = (nodes) => {
        this.allNodes = nodes;
    }

}

export default new UnlockApp();

组件:

import React from 'react';
import ReactDOM from 'react-dom';
import { observer } from 'mobx-react';
import PatternLine from './components/pattern-line/pattern-line';
import PatternScreen from './components/pattern-screen/pattern-screen';
import UnlockApp from './store';

const App = observer(() => {
    console.log(UnlockApp.currentX);
    return (
        <div>
            <PatternScreen store={UnlockApp} />
            {
                UnlockApp.patternInProgress &&
                <PatternLine lastNode={UnlockApp.lastNode}
                    currentX={UnlockApp.currentX}
                    currentY={UnlockApp.currentY} />
            }
        </div>
    )
})

有两个问题:

1) 如果我从App 组件中删除console.log(UnlockApp.currentX)AppcurrentX 更改时不再重新渲染。为什么会这样?我已经在App 中使用currentX 作为道具值,所以它不应该自动重新渲染吗?

2) 每当我按下鼠标并调用onPointPress 时,它都会成功地将patternInProgress 更新为true。由于patternInProgress 是一个observable 并在组件App 的渲染方法中使用,当patternInProgressonPointPress 方法中更改为true 时,App 会重新渲染。这也会导致PatternLine 组件的重新渲染(这是需要的)。

但是,问题在于传递给PatternLine 的道具没有被更新(而是传递了null)。即drawLine 方法没有成功更新商店内的lastNodecurrentXcurrentY

我不明白为什么会这样。我在这里做错了什么? 请注意,我没有显示调用drwaLine 方法的组件的代码。但是,我已经测试过调用组件正确地将currentXcurrentY 传递给drwaLine

【问题讨论】:

    标签: javascript reactjs mobx mobx-react


    【解决方案1】:

    您的代码有错字。当它应该说 PatternLine 时,您正在导入 PatterLine

    【讨论】:

    • 在此处粘贴代码时,n 不知何故被删除。在我的代码中正确提及。现已更正有问题。
    • 何时以及如何调用 stopCounting?
    • stopCounting 在用户释放鼠标时被调用。并且onPointPress 在用户按下鼠标而不抬起鼠标时被调用。但是,处于当前状态的应用程序永远不会调用stopCounting,因为它会在调用onPointPress 并尝试渲染PatternLine 时立即崩溃(因为传递给它的propsnull 而失败如问题中所述)。
    • 你调试过onPointPress吗?我会简单地控制台记录(点)并查看您那里有什么数据。也许将标志“patternInProgress”向上移动,看看它是否仍然中断。我的 Mobx 经验非常有限,因此无法在我的环境中对其进行测试。
    • 我发现了这个问题。基本上,patternInProgress 和其他 observables lastNodecurrentXcurrentY 正在以不同的方式更新。所以只要patternInProgress 得到更新,PatternLine 组件就会重新渲染。但是此时,其他提到的 observables 还没有更新,所以它们的值仍然是null。我可以在调用PatternLine 之前检查null 的值,就像我通过执行UnlockApp.patternInProgress &amp;&amp; &lt; PatternLine/&gt; 检查patternInProgress 一样。
    猜你喜欢
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2018-07-28
    • 2019-07-14
    • 1970-01-01
    • 2021-07-09
    相关资源
    最近更新 更多