最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React native typeScript and forwardRef in a functional component - Stack Overflow

programmeradmin3浏览0评论

I'm in react native app an I use typeScript too. I have a functional ponent :

const Input: React.FunctionComponent<IInputProps> = ({
  inputStyle,
  placeHolderColor = EAppColors.DARK_GREY,
  placeHolder,
  value,
  onChangeText,
  autoFocus,
  onFocus,
  onBlur,
  onSubmitEditing,
  ref,
  keyboardType = EKeyboardType.DEFAULT,
}) => {
  return (
    <StyledInput
      testID="TextInputID"
      placeholderTextColor={placeHolderColor}
      placeholder={placeHolder}
    ...  

I create some ref for different input before my render:

const firstNameRef = React.createRef<TextInput>();
    const lastNameRef = React.createRef<TextInput>();
    const birthDateRef = React.createRef<TextInput>();

and I use after this ponent in a class like that :

<StyledTextInput
                label={I18n.t('auth.heading.firstNameLabel')}
                errorText={errorText}
                ref={firstNameRef}
                autoFocus={true}
                placeHolder={I18n.t('auth.placeHolder.firstName')}
                isFocused={focusOnFirstFields}
                hasError={hasError}
                onFocus={() => this.setState({ focusOnFirstFields: true })}
                onBlur={() => this.setState({ focusOnFirstFields: false })}
                showClearButton={showFirstClearButton}
                value={firstName}
                onClearText={() => this.onClearText(1)}
                onChangeText={(value: string) =>
                  this.setState({
                    firstName: value,
                    disabled: false,
                    showFirstClearButton: true,
                  })
                }
                onSubmitEditing={() => {
               if (lastNameRef !== null && lastNameRef.current !== null) {
                     lastNameRef.current.focus();
                  }
                }}
                keyboardType={EKeyboardType.DEFAULT}
              />

But when I want to use onSubmitEditing for focus the next input, I have this error :

How can I resolve this issue ? Thank You!

I'm in react native app an I use typeScript too. I have a functional ponent :

const Input: React.FunctionComponent<IInputProps> = ({
  inputStyle,
  placeHolderColor = EAppColors.DARK_GREY,
  placeHolder,
  value,
  onChangeText,
  autoFocus,
  onFocus,
  onBlur,
  onSubmitEditing,
  ref,
  keyboardType = EKeyboardType.DEFAULT,
}) => {
  return (
    <StyledInput
      testID="TextInputID"
      placeholderTextColor={placeHolderColor}
      placeholder={placeHolder}
    ...  

I create some ref for different input before my render:

const firstNameRef = React.createRef<TextInput>();
    const lastNameRef = React.createRef<TextInput>();
    const birthDateRef = React.createRef<TextInput>();

and I use after this ponent in a class like that :

<StyledTextInput
                label={I18n.t('auth.heading.firstNameLabel')}
                errorText={errorText}
                ref={firstNameRef}
                autoFocus={true}
                placeHolder={I18n.t('auth.placeHolder.firstName')}
                isFocused={focusOnFirstFields}
                hasError={hasError}
                onFocus={() => this.setState({ focusOnFirstFields: true })}
                onBlur={() => this.setState({ focusOnFirstFields: false })}
                showClearButton={showFirstClearButton}
                value={firstName}
                onClearText={() => this.onClearText(1)}
                onChangeText={(value: string) =>
                  this.setState({
                    firstName: value,
                    disabled: false,
                    showFirstClearButton: true,
                  })
                }
                onSubmitEditing={() => {
               if (lastNameRef !== null && lastNameRef.current !== null) {
                     lastNameRef.current.focus();
                  }
                }}
                keyboardType={EKeyboardType.DEFAULT}
              />

But when I want to use onSubmitEditing for focus the next input, I have this error :

How can I resolve this issue ? Thank You!

Share Improve this question edited Apr 19, 2021 at 12:54 E.D asked Apr 16, 2021 at 16:58 E.DE.D 8913 gold badges17 silver badges36 bronze badges 2
  • Does this answer your question? How to use forwardRef with FunctionComponent in React Native using TypeScript – Fuze-Mcsea Commented Apr 16, 2021 at 17:30
  • thank you @Fuze-Mcsea but no :( ps: I have edit my question would be more clear – E.D Commented Apr 19, 2021 at 12:01
Add a ment  | 

3 Answers 3

Reset to default 3

Like this:

const FancyButton = React.forwardRef</* type of ref*/HTMLButtonElement, /* ponent props */ComponentProps>((props, ref) => (
  <button ref={ref} className="FancyButton">
    {props.children}
  </button>))

It will be correctly typed as

const FancyButton: React.ForwardRefExoticComponent<React.RefAttributes<HTMLButtonElement>>

(You don't need to use React.FunctionComponent when using forwardRef).

const Input = React.forwardRef<TextInput, IInputProps>(({
  inputStyle,
  placeHolderColor = EAppColors.DARK_GREY,
  placeHolder,
  value,
  onChangeText,
  autoFocus,
  onFocus,
  onBlur,
  onSubmitEditing,
  keyboardType = EKeyboardType.DEFAULT,
}, ref /* <--- ref is passed here!!*/) => {
   // assuming this is a TextInput
  return (
    <StyledInput
      ref={ref}
      testID="TextInputID"
      placeholderTextColor={placeHolderColor}
      placeholder={placeHolder}
    ... 
}) 

I faced a similar problem a few months ago. I solved it by doing:

   import {TextInputProps, TextInput} from 'react-native';

   type IProps = TextInputProps & {
      labelText?: string;
    };
    
    const TextInputStd: React.FC<IProps> = React.forwardRef(
      (
        {
          labelText,
          ...textInputProps
        }: IProps,
        ref: React.Ref<TextInput>,
      ) => {
        const {styles} = useStyles(_styles);
    
        return (
          <>
            <View style={[styles.textInputContainer, styles2.textInputContainer]}>
              <Text style={styles.labelText}>{labelText || ''}</Text>
              <View style={styles.inputWrapper}>
                <TextInput style={styles.input} {...textInputProps} ref={ref} />
              </View>
            </View>
          </>
        );
      },
    );

Hope this gives you an idea.

not 100% sure what the question is here, but

<StyledInput
  ref={ref}
  testID="TextInputID"
  placeholderTextColor={placeHolderColor}
  placeholder={placeHolder}
  ...

should work, then you need to pass the ref in when calling this input.

发布评论

评论列表(0)

  1. 暂无评论