【发布时间】:2020-12-01 21:13:34
【问题描述】:
我在更新状态时收到此错误:Error: Text strings must be rendered within a <Text> component.,这是一个包含组件的列表。我正在尝试添加一个组件,所以我不明白为什么我需要一个 <Text> 组件。此列表用于呈现抽屉组件,允许用户查看他们所属的俱乐部的页面。
const [public_list, setPublicList] = useState([]);
const [private_list, setPrivateList] = useState([]);
const [list, setList] = useState([]);
const homeIcon = <Icon name="home-outline" color={'black'} size={20} />;
var user_doc;
async function fetchData() {
user_doc = await firestore()
.collection('users')
.doc(auth_user.uid)
.get()
.catch(function (error) {
console.log(
'There has been a problem with your fetch operation: ' +
error.message,
);
});
const userData = user_doc['_data'];
let public_clubList = userData['public_clubs'];
console.log(user_doc['public_clubs']);
for (let item = 0; item < public_clubList.length; item++) {
let name = public_clubList[item]['clubName'];
const newList = list.concat('hey');
const newPublicList = public_list.concat(
<DrawerItem
icon={({color, size}) => homeIcon}
label={toString(name)}
onPress={() => {
props.navigation.navigate('ClubPage', {hello});
}}
/>,
);
setList(newList);
setPublicList(newPublicList);
console.log(name);
}
console.log(user_doc['public_clubs'][1]['clubName']);
}
错误发生在setList(newList)。我在此期间致电fetchData:
useEffect(() => {
if (list.length == 0) {
fetchData();
}
});
如果您想知道,这就是我的 userData:
{"email": "johndoe@email.com", "fullName": "John Doe", "id": "JbuhzofKDEe2ImMl9DPYpBbuVzG2", "private_clubs": [{"clubName": "Kool kids ", "id": "1903440d-e06a-4117-bc41-d27fabb80583"}, {"clubName ": "Test", "id": "53fe982f-318e-4903-a439-9e8271035393"}], "public_clubs": [{"clubName": "Testing adding users n stuff", "id": "a6cb1dcb-cfdd-48a4-b673-671519fbe6dd"}, {"clubName": "Hey guyyys", "id": "c219a611-26c3-44d3-9d66-396b0f9a738d"}], "userName": "johndoe"}
这就是我的退货声明。其余的抽屉加载正常。
return (
<View style={{flex: 1, flexDirection: 'column'}}>
<DrawerContentScrollView {...props} style={{flex: 10}}>
<DrawerItem
icon={({color, size}) => (
<Icon name="home-outline" color={'black'} size={20} />
)}
label={hello}
onPress={() => {
props.navigation.navigate('ClubPage', {hello});
}}
/>
{list}
{public_list}
{private_list}
</DrawerContentScrollView>
<Drawer.Section style={{flex: 1}}>
<DrawerItem
icon={({color, size}) => (
<Icon name="buffer" color={'black'} size={20} />
)}
label="FeedPage"
onPress={() => {
props.navigation.navigate('FeedPage');
}}
/>
</Drawer.Section>
<DrawerItem
icon={({color, size}) => (
<Icon name="pen-plus" color={'black'} size={20} />
)}
label="Make a Club"
onPress={() => {
props.navigation.navigate('MakeClub');
}}
/>
<DrawerItem
icon={({color, size}) => (
<Icon name="account" color={'black'} size={20} />
)}
label="My Profile"
onPress={() => {
props.navigation.navigate('ProfilePage');
}}
/>
</View>
);
【问题讨论】:
-
您能分享您的代码中的任何
render或return ()吗?您可能不小心在行尾添加了一个分号,呵呵 -
是的,我会编辑问题。 @ThalesKenne
标签: javascript reactjs react-native state jsx