我试图在onSubmitEditing上的输入之间切换焦点,但我无法将引用props 从我的自定义文本输入传递到主组件.

我放在ref中的控制台日志(log)没有记录任何内容.

const MyCustomInput = (props) => {
    <View> 
        <FormInput
              {...props}
              editable={!props.disabled}
              selectTextOnFocus={!props.disabled}
              disable={props.disabled}
              displayOnly={props.displayOnly}
              small={props.small}
              placeholderTextColor={'#AFAFAF'}
         />
    </View>
}
    <Controller
          control={control}
          render={({field: {onChange, onBlur, value, ref}}) => (
            <MyCustomInput
              onBlur={onBlur}
              onChangeText={(value: string) => onChange(value)}
              value={value}
              ref={(r: any) => {
                console.log('r', r);
                ref(r);
                inputRef.current = r;
              }}
            />
          )}
          name="myInput"
    />

推荐答案

解决了,我在自定义输入中缺少一个前向引用

const MyCustomInput = fowardRef((props, ref) => {
    <View> 
        <FormInput
              {...props}
              ref={ref}
              editable={!props.disabled}
              selectTextOnFocus={!props.disabled}
              disable={props.disabled}
              displayOnly={props.displayOnly}
              small={props.small}
              placeholderTextColor={'#AFAFAF'}
         />
    </View>
)}

React-native相关问答推荐

Redux将旧状态迁移到新状态

复活的夹子在Panguesture上崩溃

无法读取 @react-native-async-storage/async-storage 的未定义属性setItem

使用动画标记时如何优化react 本机 map 性能

React Navigation v5 中的初始路由参数?

react-native react-native-vector-icons:如何使用字体真棒图标

React native flexbox - 如何做 percentages || columns || responsive || grid etc

如何获取 React Native 应用程序的崩溃日志(log)?

withNavigation 只能用于导航器的视图层次 struct

用于部署后设置的 React 配置文件

React-native 在浏览器中打开链接并返回应用程序

如何在 React-Native Android 中检测拖动事件的结束

如何重置react-native 动画?

如何在react native应用程序名称中添加空格?

TouchableHighlight onPress 不起作用

React Native 中的原生广告

在不使用 3rd 方库的情况下,在react-native中显示主屏幕之前显示启动画面

react-native如何导入元素根目录?

React Native 必须双击才能使 onPress 工作

将现有的 React Native 项目转换为 Expo