You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native BLE实时绘图render无返回值及RNSVGPath报错排查

React Native BLE实时绘图报错排查方案

报错清单

  • 运行时报错:Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
  • 移除isLoading判断后触发二次报错:Error while updating property 'd' of a view managed by:RNSVGPath InvalidNumber

根因分析

首个报错触发原因

ChartScreen组件的render方法仅覆盖了isLoading === true的返回分支,当isLoading为false时无任何return语句,React无法获取待渲染节点直接抛出错误。
同时现有代码存在3处逻辑错误会直接导致状态判断失效:

  1. getDerivedStateFromProps静态方法参数写法错误,该方法标准入参为(props, state),现有代码仅传入一个参数并命名为state,实际拿到的是父组件传入的props而非组件内部state
  2. 该生命周期每次触发时返回的新state丢失了isLoading字段,会将chartData重置为仅含flow字段的对象,导致isLoading变为undefined,原有判断逻辑完全失效
  3. 初始flow数组中传入的hr是导入的React组件(CharacteristicCard),并非数值类型的BLE采样数据,本身属于非法图表数据源

二次报错触发原因

RNSVGPath的d属性要求传入合法的路径计算值,由于传给Chart组件的data数组内是React组件而非预期的数值采样点,图表组件计算SVG绘制路径时拿到非法值,直接抛出InvalidNumber错误。
另外父组件DeviceScreen渲染<ChartScreen chartdata />时未传入任何实际BLE采样数据,ChartScreen无法获取实时读数,也无法生成合法的图表绘制参数。

修复步骤

  1. 补全render方法的返回分支
    覆盖isLoading为false的渲染逻辑,确保所有判断分支都有明确return值,无内容可渲染时直接返回null即可。参考修改:
    render() {
      const { flow } = this.state.chartData;
      // 加载状态
      if (this.state.isLoading) {
        return (
          <ScreenContainer>
            <View style={styles.main}>
              <ActivityIndicator size="large" />
            </View>
          </ScreenContainer>
        );
      }
      // 加载完成渲染图表
      return (
        <ScreenContainer>
          <View style={styles.main}>
            <Chart
              key="flow"
              data={flow}
              maxValue={1900}
              minValue={1750}
              slotsPerWidth={slotsPerWidth}
              width={width}
              height={height}
              marginBottom={20}
              lineColor="rgba(95, 92, 1, 1)"
              lineThickness={2}
              chartBackground="#17204d"
              horizontalGridLinesCount={5}
              gridColor="rgba(65, 95, 93, .4)"
              gridThickness={1}
              unit="ml"
              axisTooClose={10}
              labelsColor="rgba(255, 255, 255, 0.8)"
              labelsFontSize={12}
              marginLeft={60}
              labelsMarginLeft={15}
            />
          </View>
        </ScreenContainer>
      );
    }
    
  2. 移除错误的生命周期逻辑,修正初始数据源
    现有代码中的getDerivedStateFromProps完全不符合使用场景,直接删除即可。同时修改initialState,flow初始值设为空数组,用于后续存储BLE采样的数值:
    const initialState = {
      flow: [],
      isLoading: true,
    };
    
  3. 补全BLE数据监听与传递逻辑
    在DeviceScreen中完成服务/特征值发现后,找到对应传输数据的BLE特征值开启监听,解析得到数值后更新到本地state,再通过props传递给ChartScreen:
    • 新增state存储实时采样数据:const [flowData, setFlowData] = useState([]);
    • 服务发现完成后,对对应UUID的特征开启监听:
      // 替换为你实际使用的特征UUID、数据解析方法
      const flowCharacteristic = await service.characteristic("你的特征UUID");
      flowCharacteristic.monitor((error, char) => {
        if (error) return;
        // 按照设备实际的数据格式解析数值,示例为读Float类型
        const currentValue = char.readFloatLE(0);
        setFlowData(prev => {
          // 仅保留最近slotsPerWidth个采样点,避免数组过长影响性能
          const newList = [...prev, currentValue];
          return newList.slice(-slotsPerWidth);
        });
        // 拿到第一笔数据后关闭加载态
        setIsLoading(false);
      });
      
    • 渲染ChartScreen时传入实际数据:<ChartScreen flowData={flowData} />
  4. 同步修改ChartScreen接收props更新图表数据
    在ChartScreen中监听父组件传入的flowData变化,更新到自身state的flow数组中,保证图表拿到的始终是合法的数值数组。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.01 21:57:36