【问题标题】:Debounce mobx-react and props去抖动 mobx-react 和 props
【发布时间】:2020-04-02 07:05:17
【问题描述】:

我正在尝试消除方法调用的抖动:“chart.calculateChartData(props.answers)”。

我试过了: - 自动运行 - 反应 - 从反应库中使用去抖动。 - 在calculateChartData 中设置超时

每个解决方案都会导致更新周期或无法正常工作,因为 MobX 不是不可变的。

有人提示吗?

function QuantificationChart(props: QuantificationChartProps) {
    const {t} = useTranslation();

    const rootStore = useRootStore();
    const chart = rootStore.formulaStore.getChart<Chart>(Chart.chartName);
    const data = chart.calculateChartData(props.answers);
calculateChartData = (questionData: { [id: string]: number; } = {}) => {
        let chartData = [];
        for (let i = 0; i < this.numberOfYears + 1; ++i) {
            let customData = {...questionData, "year" : i};
            let chartEntry = {
                cost: this.rootStore.formulaStore.calculateFormula(this.cost.formula, customData),
                earn: this.rootStore.formulaStore.calculateFormula(this.earn.formula, customData),
                sum: 0
            };
            chartEntry.sum = (chartEntry.earn - chartEntry.cost) + (chartData[i - 1]?.sum || 0);
            chartData.push(chartEntry);
        }
        return chartData;
    };

提示:这是我使用 MobX 的第一个项目

【问题讨论】:

    标签: mobx mobx-react


    【解决方案1】:

    找到了解决办法。似乎工作:

    基于:https://mobx-react.js.org/recipes-effects

        const [data, setData] = React.useState(chart.calculateChartData(props.answers));
    
        React.useEffect(
            () =>
                autorun(() => {
                    setData(chart.calculateChartData(props.answers));
                }, {delay: 1000}),
            [],
        );
    

    【讨论】:

      猜你喜欢
      • 2017-06-16
      • 2022-01-13
      • 2020-11-01
      • 1970-01-01
      • 2017-05-03
      • 2020-04-28
      • 2019-03-20
      • 2015-06-02
      • 2015-11-10
      相关资源
      最近更新 更多