You need to enable JavaScript to run this app. React+Redux场景下切换条形码后计算值显示异常求助
优惠活动
大模型
产品
解决方案
定价
更多

React+Redux场景下切换条形码后计算值显示异常求助

条形码切换后图表数值异常问题

数据存入Redux Store后,传入react-plotly.js的绘图函数计算并渲染图表。切换条形码再切回原条形码时,图表数值与初始状态不一致,疑似原始数组被篡改。尝试过深拷贝但问题依旧,曾用localStorage规避但因数据量超限回归Redux,新增存储已计算数据的状态也未解决问题。


相关代码

从Redux获取数据的useEffect逻辑

useEffect(() => {
    try {
        dispatch(setBarcodeCoveragePlotIsLoading(true));
        setBarcodeCoveragePlotDataPNG(undefined);

        var exists = state.barcode_coverage_plots.filter(f => f.runid === Utils.getRunIdWithoutCamera(state.runId));
        if (exists.length > 0 && exists[0].data !== undefined) {
            if (exists[0].data !== undefined) {
                dispatch(setBarcodeCoveragePlotIsLoading(false));
                var dataByBarcode = exists[0].data.data.filter(f => f.barcode === selectedBarcode);
                if (dataByBarcode && dataByBarcode.length > 0) {
                    var bc = RenderBCPlots.PlotBarcodeCoveragePlot(state.runId, dataByBarcode[0].data, dataByBarcode[0].barcode, false);
                    setBarcodeCoveragePlotData([bc]);
                    var rl = RenderReadLengthPlots.RenderReadLengthBarcodeDetailsPlot(dataByBarcode[0].data, state.runId, selectedBarcode);
                    setBarcodeDetailsReadLengthPlot(rl);
                }
            } else {
                setBarcodeCoveragePlotDataPNG(undefined);
                setRl3PlotData(undefined);
                dispatch(setBarcodeCoveragePlotIsLoading(false));
            }
        }
        else {
            setBarcodeCoveragePlotDataPNG(undefined);
            setRl3PlotData(undefined);
        }
    } catch {
        setBarcodeCoveragePlotDataPNG(undefined);
        setRl3PlotData(undefined);
    }
}, [state.barcode_coverage_plots, selectedBarcode]);

条形码切换事件处理

const handleQtableHeaderClick = barcode => {
    setSelectedBarcode(barcode);
}

图表渲染组件

{(selectedBarcode && barcodeCoveragePlotData !== undefined && barcodeCoveragePlotData.length > 0) &&
    barcodeCoveragePlotData.map((item, i) => {
        if (item !== 'object' && item !== undefined) {
            return (
                <Col lg={6} key={`BCJSON${(i + 1) * 4}`} style={{ marginTop: 15 }}>
                    <Row>
                        <Col lg={12} key={`BCJSON${(i + 1) * 2}`}>
                            <RenderPlot
                                id={`BCJSON${i}`}
                                key={`BCJSON${(i + 1) * 3}`}
                                data={item.data !== undefined && item.data}
                                layout={item.layout !== undefined && item.layout}
                                config={item.config !== undefined && item.config}
                                isLoading={state.is_barcode_coverage_plot_loading}
                                type={'rounded'}
                                width={'100%'}
                                height={400}
                            />
                        </Col>
                    </Row>
                </Col>
            );
        }
    })
}

解决方案

1. 强制深拷贝隔离原始数据

问题核心大概率是绘图函数直接修改了传入的原始数据(Redux存储的源数据),即使之前做过深拷贝,可能存在层级覆盖不全的情况。修改useEffect中的数据传递逻辑,确保每次传入绘图函数的是完整深拷贝后的副本:

// 可以用lodash的cloneDeep,或者自定义深拷贝函数
import cloneDeep from 'lodash/cloneDeep';

// ... 其他代码 ...
if (dataByBarcode && dataByBarcode.length > 0) {
    // 对每个绘图函数的输入数据单独深拷贝
    const clonedCoverageData = cloneDeep(dataByBarcode[0].data);
    var bc = RenderBCPlots.PlotBarcodeCoveragePlot(state.runId, clonedCoverageData, dataByBarcode[0].barcode, false);
    setBarcodeCoveragePlotData([bc]);

    const clonedReadLengthData = cloneDeep(dataByBarcode[0].data);
    var rl = RenderReadLengthPlots.RenderReadLengthBarcodeDetailsPlot(clonedReadLengthData, state.runId, selectedBarcode);
    setBarcodeDetailsReadLengthPlot(rl);
}

如果不想引入lodash,可自定义深拷贝函数:

const deepClone = (obj) => {
    if (typeof obj !== 'object' || obj === null) return obj;
    if (obj instanceof Array) return obj.map(item => deepClone(item));
    return Object.fromEntries(Object.entries(obj).map(([key, val]) => [key, deepClone(val)]));
};

2. 检查Redux Reducer的纯函数性

确保处理barcode_coverage_plots的Reducer没有直接修改原始state,必须返回全新的对象/数组:

// 错误示例:直接修改原数组
state.barcode_coverage_plots.push(newPlot);

// 正确示例:返回新状态对象
return {
    ...state,
    barcode_coverage_plots: [...state.barcode_coverage_plots, newPlot]
};

3. 排查绘图函数内部逻辑

检查PlotBarcodeCoveragePlot和RenderReadLengthBarcodeDetailsPlot内部是否存在直接修改输入数据的代码,例如:

// 错误:直接修改输入数组
data.forEach(item => item.value *= 2);

// 正确:返回处理后的新数组
const processedData = data.map(item => ({...item, value: item.value * 2}));

如果是第三方库无法修改,必须通过深拷贝隔离源数据。


内容的提问来源于stack exchange,提问作者Duex39

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 10:35:38