ReactNative 制作一个数字输入自定义组件

huangapple go评论68阅读模式
英文:

ReactNative making a numeric input custom component

问题

我正在尝试创建一个numericInput组件,我试图通过移除RNTextInput来重构下面的代码,但我无法理解它在以下部分使用ref的原因:

React.forwardRef<RNTextInput, Props>((props, ref) => {
    const { onChangeText, ...rest } = props;

为什么我们要传递ref?

import React, { useCallback } from "react";
import { TextInput as RNTextInput, StyleSheet, TextInputProps, View } from "react-native";
import { BaseTextInput } from "src/components/atoms/BaseTextInput";
import { colorsConst } from "src/styles/const/colorsConst";

type Props = Omit<TextInputProps, "keyboardType" | "selectionColor" | "autoCapitalize"> & {
  disabled?: boolean;
};

export const NumericInput = React.memo(
  React.forwardRef<RNTextInput, Props>((props, ref) => {
    const { onChangeText, ...rest } = props;

    const onChanged = useCallback(
      (text: string) => {
        return onChangeText ? onChangeText(text.replace(/[^0-9]/g, "")) : undefined;
      },
      [onChangeText],
    );

    return (
      <View style={styles.container}>
        <BaseTextInput
          ref={ref}
          style={[
            styles.text,
            {
              ...(rest.disabled ? { color: colorsConst.DISABLED } : {}),
            },
          ]}
          {...rest}
          keyboardType="number-pad"
          onChangeText={onChanged}
        />
      </View>
    );
  }),
);

NumericInput.displayName = "NumericInput";

const styles = StyleSheet.create({
  container: {
    borderRadius: 5,
    borderWidth: 1,
    borderColor: colorsConst.DIVIDER,
  },
  text: {
    width: "100%",
    fontSize: 14,
    paddingHorizontal: 18,
  },
});
英文:

I am trying to make a numericInput component and I was trying to refactor the below code by removing RNTextInput. i couldnot understand the part where it was using

React.forwardRef&lt;RNTextInput, Props&gt;((props, ref) =&gt; {
    const { onChangeText, ...rest } = props;

why do we pass ref?

import React, { useCallback } from &quot;react&quot;;
import { TextInput as RNTextInput, StyleSheet, TextInputProps, View } from &quot;react-native&quot;;
import { BaseTextInput } from &quot;src/components/atoms/BaseTextInput&quot;;
import { colorsConst } from &quot;src/styles/const/colorsConst&quot;;

type Props = Omit&lt;TextInputProps, &quot;keyboardType&quot; | &quot;selectionColor&quot; | &quot;autoCapitalize&quot;&gt; &amp; {
  disabled?: boolean;
};

export const NumericInput = React.memo(
  React.forwardRef&lt;RNTextInput, Props&gt;((props, ref) =&gt; {
    const { onChangeText, ...rest } = props;

    const onChanged = useCallback(
      (text: string) =&gt; {
        return onChangeText ? onChangeText(text.replace(/[^0-9]/g, &quot;&quot;)) : undefined;
      },
      [onChangeText],
    );

    return (
      &lt;View style={styles.container}&gt;
        &lt;BaseTextInput
          ref={ref}
          style={[
            styles.text,
            {
              ...(rest.disabled ? { color: colorsConst.DISABLED } : {}),
            },
          ]}
          {...rest}
          keyboardType=&quot;number-pad&quot;
          onChangeText={onChanged}
        /&gt;
      &lt;/View&gt;
    );
  }),
);

NumericInput.displayName = &quot;NumericInput&quot;;

const styles = StyleSheet.create({
  container: {
    borderRadius: 5,
    borderWidth: 1,
    borderColor: colorsConst.DIVIDER,
  },
  text: {
    width: &quot;100%&quot;,
    fontSize: 14,
    paddingHorizontal: 18,
  },
});

答案1

得分: 1

这是代码的翻译部分:

const MyTextInput = React.forwardRef((props, ref) => {
  return <TextInput ref={ref} {...props} />;
});

const App = () => {
  const inputRef = useRef(null);
  
  useEffect(() => {
    inputRef.current?.focus();
  }, [inputRef]);

  return <MyTextInput ref={inputRef} />;
};

请注意,翻译中保留了代码的格式和结构,但翻译为中文的代码本身没有实际意义。

英文:

You pass it so that you can assign the ref at the level of the consuming component. Refs have to be passed outside of normal props because they are references to DOM nodes. You can read more about this in the docs for forwardRef.

That's how you can call inputRef.current.focus() (for example) on a custom text input. You create the ref where you use the custom component, and the custom component forwards that ref to the TextInput. Pseudocode:

const MyTextInput = React.forwardRef((props, ref) =&gt; {
  return &lt;TextInput ref={ref} {...props} /&gt;;
});

const App = () =&gt; {
  const inputRef = useRef(null);
  
  useEffect(() =&gt; {
    inputRef.current?.focus():
  }, [inputRef]);

  return &lt;MyTextInput ref={inputRef} /&gt;;
};

huangapple
  • 本文由 发表于 2023年2月19日 15:00:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定