【发布时间】:2020-08-17 18:25:40
【问题描述】:
我正在从实时数据库 (firebase) 中检索一些数据,但是,当我尝试在数据库引用调用之外检查“级别”的值时,我得到一个未定义的值,尽管它在引用中工作。
有没有办法允许访问 react-native 组件中的变量 'levels'?
我在网上看到了一些使用 this.setState 来实现此目的的示例,但似乎没有任何方法可以在函数中执行此操作。
function LevelsScreen() {
const reference = database()
.ref()
.once('value')
.then(snapshot => {
//get Levels from db
let levels = [];
snapshot.forEach((child) => {
levels.push(child.key);
})
console.log(levels);
});
return (
<SafeAreaView style={styles.categories}>
{/* Horizontal Scrollbox for Main Categories */}
<ScrollView horizontal={true} style={styles.scrollView}>
<View>
{/* This function should call LevelCard to return the components with the proper titles*/}
{levels.map(element => {
return (<LevelCard
level={element}/>)
})
}
</View>
</ScrollView>
</SafeAreaView>
);
}
【问题讨论】:
标签: android reactjs function react-native mobile