【发布时间】:2021-08-02 12:14:38
【问题描述】:
我只是想知道,是否可以在 useEffect 挂钩中渲染组件。 我有两个组件,比如说 A 和 B。 在组件 A 中,我的 useEffect 挂钩如下。 在测试钩子内的某些条件时,我想渲染一个组件 B。
export default function A() {
const [location, setlocation] = usestate(null);
useEffect(() => {
if (condition) {
<B />; // it does not render anything though
} else {
//code
}
}, [location]);
}
export default function B() {
const test = () => {
console.log("testing");
};
return (
<View style={styles.container}>
<Button title={"Click Me"} onPress={test} />
</View>
);
}
【问题讨论】:
-
我不这么认为,但是您可以更新状态并有条件地渲染组件
-
你必须在你的 useEffect 钩子之后有条件地返回 。
-
功能组件呈现它们的返回值。任何不在返回值中的东西都不会被渲染。
标签: javascript reactjs react-native react-hooks expo