Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取Firebase数据,将其存储到数组中,然后使用flatlist显示它?_Firebase_React Native_React Native Flatlist - Fatal编程技术网

如何获取Firebase数据,将其存储到数组中,然后使用flatlist显示它?

如何获取Firebase数据,将其存储到数组中,然后使用flatlist显示它?,firebase,react-native,react-native-flatlist,Firebase,React Native,React Native Flatlist,我正在从firebase获取数据并将其存储到阵列中。现在我想用flatlist显示它们,但我不知道如何显示 class HomeScreen extends Component{ constructor(props){ super(props); this.state={ menu:[] } } // componentDidMount() { // firebase.auth().onAuthStateChanged(authenti

我正在从firebase获取数据并将其存储到阵列中。现在我想用flatlist显示它们,但我不知道如何显示

class HomeScreen extends Component{

  constructor(props){
    super(props);
    this.state={
      menu:[]
    }
  }

  // componentDidMount() {
  //   firebase.auth().onAuthStateChanged(authenticate => {
  //     if (authenticate) {
  //       this.props.navigation.replace("Home");
  //     } else {
  //       this.props.navigation.replace("login");
  //     }
  //   });
  // }

   componentDidMount() {
    firebase.database().ref('menu/starter/').once('value').then(snapshot => {
       var items = [];
       snapshot.forEach((child) => {
         items.push({
            name: child.val().name,
            image: child.val().image,
            price: child.val().price,
         });
      });
      this.setState({ menu: items});
      console.log(this.state.menu)
  });
  }

  render(){
    return(
     <View style={styles.container}>
     <Text></Text>
     </View>
    )
  }
}
类主屏幕扩展组件{
建造师(道具){
超级(道具);
这个州={
菜单:[]
}
}
//componentDidMount(){
//firebase.auth().onAuthStateChanged(authenticate=>{
//如果(验证){
//此.props.navigation.replace(“Home”);
//}其他{
//this.props.navigation.replace(“登录”);
//     }
//   });
// }
componentDidMount(){
firebase.database().ref('menu/starter/')。一次('value')。然后(快照=>{
var项目=[];
snapshot.forEach((子项)=>{
推({
名称:child.val().name,
image:child.val().image,
price:child.val().price,
});
});
this.setState({menu:items});
console.log(this.state.menu)
});
}
render(){
返回(
)
}
}
elem.name}
renderItem={elem=>({elem.item.name})}
/>

renderItem是用于渲染每个数组项的回调

谢谢。我忘了拔钥匙器。谢谢你的解决方案。
<FlatList 
data={this.state.menu}
keyExtractor={elem => elem.name}
renderItem={elem => (<View><Text>{elem.item.name}</Text></View>)}
/>