React - display a firestore timestamp

后端 未结 6 2065
醉酒成梦
醉酒成梦 2020-12-18 22:38

I am trying to figure out how to display a firestore timestamp in a react app.

I have a firestore document with a field named createdAt.

I am trying to inc

6条回答
  •  盖世英雄少女心
    2020-12-18 23:36

    I've encountered problems in the past with nested object properties not rendering properly in lists where item.someProp is considered an object, but item.someProp.someSubProp won't resolve with the value of someSubProp in item.someProp.

    So to skirt around the issue, why not evaluate Timestamp to a plain date object (or the desired display format) when creating the user object?

    this.unsubscribe = this.props.firebase
      .users()
      .onSnapshot(snapshot => {
        let users = [];
    
        snapshot.forEach(doc =>
          let docData = doc.data();
          docData.createdAt = docData.createdAt.toDate();
          users.push({ ...docData, uid: doc.id }),
        );
    
        this.setState({
          users,
          loading: false,
        });
      });
    

提交回复
热议问题