简体   繁体   English

将下一个文本输入集中在功能组件中的本机反应中

[英]focus next textInput in react native in functional component

i have a component for textinput我有一个文本输入组件

return (
    <TextInput
      onChangeText={(text) => {
        props.onChange(text)
      }}
      value={props.value}
      placeholder={props.placeholder}
      placeholderTextColor={Colors.gry}
      autoFocus={props.focus ? true : false}
      onFocus={() => setColor(Colors.org)}
      onBlur={() => setColor(Colors.gry)}
      ref={props.ref}
      returnKeyType='next'
    />
  )

i'm using this component like this我正在像这样使用这个组件

                   const inputRef = useRef<any>()

                    <Input
                      keyboard={'numeric'}
                      onChange={(text) => {
                        setOtp({ ...otp, o1: text })
                        inputRef.current.focus()
                      }}
                      value={otp.o1}
                      max={1}
                    />
                  </View>
                  <View style={styles.input}>
                    <Input
                      keyboard={'numeric'}
                      onChange={(text) => setOtp({ ...otp, o2: text })}
                      value={otp.o2}
                      ref={inputRef}
                      max={1}
                    />
                  </View>

i want to focus next input field when change text in first input field how can i do it?我想在第一个输入字段中更改文本时关注下一个输入字段,我该怎么做?

Since your component Input is a wrapper for TextInput you need to use forwardRef to pass the ref to inner child and not directly through props由于您的组件 Input 是 TextInput 的包装器,因此您需要使用forwardRef将 ref 传递给 inner child 而不是直接通过 props

here is a code snipprt from react documentation on how to do it这是有关如何操作的反应文档中的代码片段

const Input = React.forwardRef((props, ref) => (

<TextInput
  onChangeText={(text) => {
    props.onChange(text)
  }}
  value={props.value}
  placeholder={props.placeholder}
  placeholderTextColor={Colors.gry}
  autoFocus={props.focus ? true : false}
  onFocus={() => setColor(Colors.org)}
  onBlur={() => setColor(Colors.gry)}
  ref={ref} 
  returnKeyType='next'
/>)

// You can now get a ref directly :
const inputRef = React.createRef();
 <Input
     keyboard={'numeric'}
     onChange={(text) => setOtp({ ...otp, o2: text })}
     value={otp.o2}
     ref={inputRef}
     max={1}

then you can use focus然后你可以使用焦点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React Native 的 TextInput 组件中获取动态引用和焦点 - How to get dynamic ref and focus in TextInput component on React Native 如何将 TextInput 集中在 React Native 中的按钮按下上 - How to focus TextInput on button press in React Native 如何将 cursor 聚焦到 React 可拖动菜单中的下一个 TextInput? - How can I focus the cursor to the next TextInput in a React Draggable Menu? React Native:无法使用 ref 在 returnKeyType="next" 上切换到下一个 TextInput - React Native : Not able to switch to next TextInput on returnKeyType="next" using ref React Native 和 TypeScript:“RefObject”类型上不存在属性“焦点”<TextInput> &#39; - React Native & TypeScript : Property 'focus' does not exist on type 'RefObject<TextInput>' 测试依赖于 Promise 的 React Native 功能组件 - Testing React Native Functional Component With Dependency on Promise 在功能组件中反应原生 typeScript 和 forwardRef - React native typeScript and forwardRef in a functional component 如何在 React Native 中关注下一个字段输入? - How focus the next field input in react native? React Native TextInput 包装器组件,如何从“外部”清除值 - React Native TextInput wrapper component, how to clear the value from "outside" 反应原生 unfocus TextInput - react native unfocus TextInput
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM