使用redux-form按下下一个键盘按钮后如何使用useRef钩子选择下一个TextInput

shu*_*ary 8 textinput reactjs react-native redux-form react-hooks

我知道我们可以使用 react 类方法轻松做到这一点。因为我们有 this.ref。但我不确定如何在功能组件中使用 useRef 钩子来做到这一点。

使用这里写的技巧

这就是我试图做到这一点的方式。

  ...

  const inputEl1 = useRef(null);
  const inputEl2 = useRef(null);
  return (
        <Field
            name="first_name"
            component={MyTextInput}
            placeholder="First name"
            ref={inputEl1}
            refField={inputEl1}
            onEnter={() => {
              inputEl2.current.focus();
            }}
          />
          />
          <Field
            name="last_name"
            placeholder="Last name"
            component={MyTextInput}
            ref={inputEl2}
            refField={inputEl2}
          />
)
...
Run Code Online (Sandbox Code Playgroud)

所以我需要将 ref from field 传递给 MyTextInput 并且在 nextKeyPress 上我想关注第二个输入组件,即 inputEl2

// 我的文本输入

...
render() {
    const {
      input: { value, onChange, onBlur },
      meta: { touched, error },
      keyboardType,
      placeholder,
      secureTextEntry,
      editable,
      selectTextOnFocus,
      style,
      selectable,
      customValue,
      underlineColorAndroid,
      autoFocus,
      maxLength,
      returnKeyType,
      onEnter,
      refField,
    } = this.props;
    const { passwordVisibility, undelineColor } = this.state;

    return (
      <View style={{ marginVertical: 8 }}>
        <TextInput
          style={[{
            height: 50,
            paddingLeft: 20,
            color: Colors.SECONDARY_COMMON,
          }, style]}
          onBlur={val => onBlur(val)}
          keyboardType={keyboardType}
          underlineColorAndroid={undelineColor}
          placeholder={placeholder}
          returnKeyType={returnKeyType || 'go'}
          value={customValue || value.toString()}
          onChangeText={onChange}
          maxLength={maxLength}
          onSubmitEditing={onEnter}
          ref={refField}
        />
)
}
Run Code Online (Sandbox Code Playgroud)

小智 8

const inputEl2 = useRef(null);
<TextInput
        name="first_name"           
        placeholder="First name"
        onSubmitEditing={() => inputEl2.current.focus()}
      />

<TextInput
        name="last_name"
        placeholder="Last name"
        ref={inputEl2}
      />
Run Code Online (Sandbox Code Playgroud)

它对我有用


Dyl*_*n w 5

如果它是您尝试获取 ref 的子组件,则需要将 prop 作为其他名称而不是ref.

这个方法对我有用

对于钩子,useRef 来初始化 ref。

const passwordInput = useRef(null);
Run Code Online (Sandbox Code Playgroud)

为自定义组件的 ref prop 使用不同的名称,例如 inputRef。

<CustomInput
  inputRef={ passwordInput }
/>
Run Code Online (Sandbox Code Playgroud)

子组件 - 将 ref 设置为自定义 ref 道具。

const CustomInput = props => {
  const { inputRef } = props;
  
  return ( <TextInput
    { ...props }
    ref={ inputRef }
  /> );
};
Run Code Online (Sandbox Code Playgroud)


Apr*_*ion 1

如果孩子需要使用像 ref 这样的 prop <TextInput ref={refField}...,那么 prop 必须是 ref (而不是字符串):

<Field refField={inputEl2} ...
Run Code Online (Sandbox Code Playgroud)