简体   繁体   English

无法在 React Final Form 中使用 forwardRef

[英]Unable for forwardRef in React Final Form

Using react-final-form i am unable to forwardRef to my TextInput for React Native.使用react-final-form我无法为 React Native forwardRef到我的TextInput The ref is required for handling the Next button in keyboards and other forced-focus events in forms.需要ref来处理键盘中的Next按钮和表单中的其他强制聚焦事件。

My setup is as follows - please note that some code has been removed for simplicity so this won't cut and paste.我的设置如下 - 请注意,为了简单起见,一些代码已被删除,因此不会剪切和粘贴。

// FormInputGroup/index.js

import React from 'react'
import PropTypes from 'prop-types'
import { Field } from 'react-final-form'

const FormInputGroup = ({ Component, validate, ...rest }) => (
  <Field component={Component} validate={validate} {...rest} />
)

FormInputGroup.propTypes = {
  name: PropTypes.string.isRequired,
  Component: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
  validate: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
}

export default FormInputGroup
// Form/index.js

import { Form } from 'react-final-form'
import { FormInputGroup, FormInputText, ButtonPrimary } from '/somewhere/'
...

const passwordInputRef = useRef()

<Form
  onSubmit={({ email, password }) => {
    // submit..
  }}
>
  {({ handleSubmit }) => (
    <>
      <FormInputGroup
        name="email"
        Component={FormInputText}
        returnKeyType="next"
        onSubmitEditing={() => passwordInputRef.current.focus()}
        blurOnSubmit={false}
      />
      <FormInputGroup
        name="password"
        Component={FormInputText}
        returnKeyType="go"
        onSubmitEditing={handleSubmit}
        ref={passwordInputRef} // <-- This does not work which i think is to be expected...
      />
      <ButtonPrimary loading={loading} onPress={handleSubmit}>
        Submit
      </ButtonPrimary>
    </>
  )}
</Form>

...
// FormInputText/index.js

const FormInputText = forwardRef( // <-- added forwardRef to wrap the component
  (
    {
      input,
      meta: { touched, error },
      label,
      ...
      ...rest
    },
    ref,
  ) => {
    return (
      <View style={styles.wrapper}>
        {label ? (
          <Text bold style={styles.label}>
            {label}
          </Text>
        ) : null}
        <View style={styles.inputWrapper}>
          <TextInput
            onChangeText={input.onChange}
            value={input.value}
            ...
            ref={ref}
            {...rest}
          />
        </View>
      </View>
    )
  },
)

I think the code falls down with the <FormInputGroup /> component.我认为代码与<FormInputGroup />组件有关。 A clue to this is if i change the rendering on the form to be as follows;对此的一个线索是,如果我将表单上的渲染更改为如下所示;

...

     <FormInputGroup
        name="password"
        Component={props => <FormInputText {...props} ref={passwordInputRef} />} // <-- changed
        returnKeyType="go"
        onSubmitEditing={handleSubmit}
      />

...

This does seem to forward the ref, but "breaks" final-form with each keystroke dismissing the keyboard, presumably due to a re-render.这似乎确实转发了 ref,但每次击键都会“破坏” final-form ,这可能是由于重新渲染。

You didn't forward the ref from FormInputGroup .您没有转发来自FormInputGroup的引用。

You need to capture the ref at the level it is passed down and further forward it to the children.您需要在传递的级别捕获 ref 并将其进一步转发给孩子们。 This is how it should be:应该是这样:

const FormInputGroup = forwardRef(({ Component, validate, ...rest }, ref) => (
  <Field ref={ref} component={Component} validate={validate} {...rest} />
))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM