【发布时间】:2020-11-23 08:38:42
【问题描述】:
我想将 value 和 setValue 传递给 Right 组件。我做了一些事情,但它不起作用。我正在输入,但它会立即删除。我什至看不到我在 textinput 中输入的内容。这样做的正确方法是什么?
export const Vault = ({ navigation }: VaultStackNavigationProps<"Vault">) => {
const [value, setValue] = useState("");
React.useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<Right
setText={setValue}
value={value}
/>
),
});
}, [navigation]);
return (
//component style
);
};
const Right = ({ value, setText }) => {
const [focus, setFocus] = useState(false);
const { width } = useWindowDimensions();
const onSearch = () => {
setFocus(true);
};
const onClose = () => {
setFocus(false);
};
return (
<>
<Animated.Viewstyle={{flexDirection: "row",justifyContent: "center",alignItems: "center",width:width - 40,
}}
>
{focus && (
<TextInput
value={value}
onChangeText={(text) => setText(text)}
placeholder="Type here"
/>
)}
{value.length > 0 && (
<TouchableOpacity style={{ width: width / 9 }} onPress={onClear}>
<AntDesign name="close" size={24} color="white" />
</TouchableOpacity>
)}
</Animated.View>
{!focus && (
<TouchableOpacity onPress={onSearch} style={{ width: width / 9 }}>
<AntDesign name="search1" size={24} color="#64646E" />
</TouchableOpacity>
)}
</>
);
};
【问题讨论】:
标签: react-native state react-props