英文:
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<RNTextInput, Props>((props, ref) => {
const { onChangeText, ...rest } = props;
why do we pass 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,
},
});
答案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) => {
return <TextInput ref={ref} {...props} />;
});
const App = () => {
const inputRef = useRef(null);
useEffect(() => {
inputRef.current?.focus():
}, [inputRef]);
return <MyTextInput ref={inputRef} />;
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论