remaxjs / remax

使用真正的 React 构建跨平台小程序
https://remaxjs.github.io/remax/
MIT License
4.57k stars 364 forks source link

createNativeComponent 现在的实现容易因为没有 ref 报 warning #478

Closed LeoYuan closed 4 years ago

LeoYuan commented 4 years ago

bug 描述

remax 对原生小程序组件会通过 React.forwardRef 进行一层封装,其中会通过 props.ref 往原生组件「传递」ref,在生成的 axml 中,也会通过类似 `<form-0 ref="{{item.props['ref']}}">` 获取 ref~

此时有个断言是,用户在使用小程序封装的组件时一定会传入 ref,但很可能没传,从而导致截图中的 warning,建议可以在用户没有传 ref 的时候,mock 一个实现,如:props.__ref = () => {}

复现步骤 使用任意一个自定义组件即可复现

期望结果 没有 warning

复现代码 😁

版本信息:

其他信息

image

Darmody commented 4 years ago

复现代码是怎么样的

LeoYuan commented 4 years ago

随便引用一个自定义组件就能复现~

import Badge from 'mini-antui/es/badge';

<View>
  <Badge><View slot="inner">Remax</View></Badge>
</View>

根因就是 axml 如 <form-0 ref={{item.props.__ref}} /> 中的 item.props.__ref 出现了 undefined,就会出现这个 warning~

Darmody commented 4 years ago

我试了下没有警告额