【问题标题】:How to fetch Firebase data, store it Into an array and then display it using flatlist?如何获取 Firebase 数据,将其存储到数组中,然后使用 flatlist 显示它?
【发布时间】:2019-09-23 14:36:45
【问题描述】:

我正在从 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>
    )
  }
}

【问题讨论】:

    标签: firebase react-native react-native-flatlist


    【解决方案1】:
    <FlatList 
    data={this.state.menu}
    keyExtractor={elem => elem.name}
    renderItem={elem => (<View><Text>{elem.item.name}</Text></View>)}
    />
    

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

    【讨论】:

    • 谢谢伙计。我忘记了keyExtractor。感谢您的解决方案。
    猜你喜欢
    • 2015-08-31
    • 2017-04-21
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    相关资源
    最近更新 更多