[英]Conditional rendering with React Hooks : loading
I am learning how to use React Hooks and have been stuck for many hours on something that's supposed to be very simple.我正在学习如何使用 React Hooks,并且在一些本应非常简单的事情上被困了好几个小时。 I am trying to display aa text if the state variable "loading" is true.
如果 state 变量“正在加载”为真,我正在尝试显示文本。 If it's false, I want to display something else.
如果它是假的,我想显示别的东西。 No matter what I try, "loading" is always false or at least, the UI does not appear to reflect its value.
无论我尝试什么,“加载”总是错误的,或者至少,UI 似乎没有反映它的价值。
here is the code:这是代码:
import React, {useState, useEffect} from 'react';
import {View, SafeAreaView, Text} from 'react-native';
const testScreen= (props) => {
const [loading, setLoading ] = useState(true);
useEffect(() => {
setLoading(false);
}, []);
if(loading)
{
return <Text>Hi</Text>;
}
else
{
return<Text.Hey</Text>
}
}
export default testScreen;
Any help will be more than welcome and I am sorry if this is very basic.任何帮助都将受到欢迎,如果这是非常基本的,我很抱歉。
UPDATE: Here is the actual code I am working with.更新:这是我正在使用的实际代码。 SetLoading is supposed to update the state variable to false but never does or at least, the UI des not render.
SetLoading 应该将 state 变量更新为 false 但永远不会或至少不会渲染 UI。
import React, {useState, useEffect} from 'react';
import {View, SafeAreaView, Text, ActivityIndicator} from 'react-native';
import CategoryTag from '../Components/CategoryTag';
import firestore from '@react-native-firebase/firestore';
const CategoryScreen = (props) => {
const topicCollection = firestore().collection('Topics')
.where("active","==",true);
//hooks for topics
const [topics,setTopics] = useState([]);
const [loading, setLoading ] = useState(true);
//get all active topics
useEffect(() => {
return topicCollection.onSnapshot(querySnapshot => {
const list = [];
querySnapshot.forEach(doc => {
const { active, name } = doc.data();
list.push({
id: doc.id,
active,
name,
});
});
setTopics(list);
setLoading(false);
});
}, []);
const renderTopics = () =>{
return(
topics.map((item) =>{
return(
<CategoryTag key = {item.id}
color={userTopics.includes(item.name) ?"#51c0cc":"#303239"}
name = {item.name}
isSelected = {userTopics.includes(item.name)}
handleClick = {addTopicToUserTopicCollection}
/>
)
})
)
}
if(loading)
{
return (
<SafeAreaView style={{flex:1, backgroundColor:"#455a65"}}>
<View style={{width:200, padding:20, paddingTop:60}}>
<Text style ={{fontSize:25, fontWeight:"bold",
color:"#fff"}}>What are you</Text>
<Text style ={{fontSize:22, color:"#fff"}}>interested in?
</Text>
</View>
<View style={{flex:1, alignItems:"center",
justifyContent:"center", alignSelf:"center"}}>
<ActivityIndicator />
</View>
</SafeAreaView>
)
}
else // this never runs
{
return (
<SafeAreaView style={{flex:1, backgroundColor:"#455a65"}}>
<View>
<View style={{width:200, padding:20, paddingTop:60}}>
<Text style ={{fontSize:25, fontWeight:"bold",
color:"#fff"}}>What are you</Text>
<Text style ={{fontSize:22, color:"#fff"}}>interested in?
</Text>
</View>
<View style ={{flexDirection:"column", paddingTop:20}}>
<View style ={{padding:15, paddingTop:15,
marginBottom:15,
flexWrap:"wrap", flexDirection:"row"}}>
{renderTopics(topics)}
</View>
</View>
</View>
</SafeAreaView>
);
}
}
export default CategoryScreen;
You are immediately setting your setLoading
state to false and therefore loading text might be rendering for fraction of second, or not at all, like a glitch.您立即将
setLoading
state 设置为 false,因此加载文本可能会在几分之一秒内呈现,或者根本不会呈现,就像一个小故障。 Try setting setLoading
with a timeout and then you will see the intended behaviour.尝试将
setLoading
设置为超时,然后您将看到预期的行为。
const TestScreen= (props) => { const [loading, setLoading ] = useState(true); useEffect(() => { setTimeout(()=>setLoading(false), 3000); }, []); if(loading) { return <Text>Hi</Text>; } else { return<Text>hey</Text> } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.