简体   繁体   English

React JS 将 firebase 中的数据传递到我的组件的 props 中

[英]React JS passing data from firebase into props for my component


class Mailbox extends Component {
  componentDidMount() {
    const db = firebase.firestore();
    firebase.auth().onAuthStateChanged(function (user) {
      if (user) {
        var user = firebase.auth().currentUser.uid;
        let cityRef = db.collection("Users").doc(user);
        let getDoc = cityRef
          .get()
          .then((doc) => {
            if (!doc.exists) {
              console.log("No such document!");
            } else {
              console.log("Document data:", doc.data());
              let data = doc.data();
              console.log(data.EmailBody);
              console.log(data.EmailSubject);
            }
          })
          .catch((err) => {
            console.log("Error getting document", err);
          });
      }
    });
  }
  render() {
    return (
      <>
        <div id="ZenTitle">
          <h1>ZenMail Mailbox</h1>
        </div>

        <div id="MainArea" className="MainArea">
          <Popup
            trigger={
              <button className="ComposeButton">
                <i class="fas fa-pen-square" style={{ color: "white" }}></i>{" "}
                Compose
              </button>
            }
            position="center center"
            contentStyle={{
              backgroundColor: "#465775",
              width: "98%",
              height: "80%",
              color: "white",
              paddingTop: "25px",
              borderRadius: "25px",
            }}
            arrowStyle={{
              display: "none",
            }}
          >
            <div>Compose New Email</div>
            <br />
            <NormEdit />
          </Popup>

          <Button className="RefreshButton">
            <i style={{ color: "black" }} class="fas fa-sync"></i>
          </Button>
          <Button className="FavButton">
            <i style={{ color: "black" }} class="fas fa-star"></i>
          </Button>
          <Button className="DeleteButton">
            <i style={{ color: "black" }} class="fas fa-trash-alt"></i>
          </Button>
          <Form>
            <FormGroup>
              <Label></Label>
              <Input className="EmailSearch" placeholder="Search Emails..." />
            </FormGroup>
          </Form>

          <Popup
            trigger={
              <button className="SettingsButton">
                <i style={{ color: "black" }} class="fas fa-cog"></i>
              </button>
            }
            position="right center"
            contentStyle={{
              backgroundColor: "#465775",
              width: "98%",
              height: "80%",
              color: "white",
              paddingTop: "25px",
              borderRadius: "25px",
            }}
            arrowStyle={{
              display: "none",
            }}
          >
            <div>Account Settings</div>
          </Popup>
          <Inbox />
          <div className="IncomingArea">
            <SingleEmail From="Testing" Subject="Testing" Date="testing" />
          </div>


So basically what I am trying to do here is I am trying to pass my所以基本上我在这里要做的是我试图通过我的


      data.EmailBody
      data.EmailSubject

from my componentDidMount as props down into my从我的 componentDidMount 作为道具向下进入我的


 <SingleEmail From="Testing" Subject="Testing" Date="testing" />


but am struggling to figure out how to do so.但我正在努力弄清楚如何做到这一点。 The few ways I tried do not seem to be working.我尝试的几种方法似乎不起作用。 I am somewhat new to react and I tried doing it with static props but it does not seem to be possible to do it that way from what I could tell.我对反应有些陌生,我尝试使用 static 道具来做到这一点,但据我所知,这样做似乎是不可能的。 I would appreciate any suggestions =] Hope everyone is staying safe我将不胜感激任何建议=]希望每个人都保持安全

use state and set the state using setState when you get the data then pass it on to the component.获取数据时使用state并使用setState设置 state 然后将其传递给组件。

Like this:像这样:

class Mailbox extends Component {
    state = {
        data: {
            EmailBody: '', 
            EmailSubject: '', 
        }
    };

    componentDidMount() {
        const db = firebase.firestore();
        firebase.auth().onAuthStateChanged(function (user) {
            if (user) {
                var user = firebase.auth().currentUser.uid;
                let cityRef = db.collection("Users").doc(user);
                let getDoc = cityRef
                    .get()
                    .then((doc) => {
                        if (!doc.exists) {
                            console.log("No such document!");
                        } else {
                            console.log("Document data:", doc.data());
                            let data = doc.data();
                            this.setState({
                                data,

                            })
                            console.log(data.EmailBody);
                            console.log(data.EmailSubject);
                        }
                    })
                    .catch((err) => {
                        console.log("Error getting document", err);
                    });
            }
        });
    }
    render() {
        return (
            <>
                <div id="ZenTitle">
                    <h1>ZenMail Mailbox</h1>
                </div>

                <div id="MainArea" className="MainArea">
                    <Popup
                        trigger={
                            <button className="ComposeButton">
                                <i class="fas fa-pen-square" style={{ color: "white" }}></i>{" "}
                                Compose
                            </button>
                        }
                        position="center center"
                        contentStyle={{
                            backgroundColor: "#465775",
                            width: "98%",
                            height: "80%",
                            color: "white",
                            paddingTop: "25px",
                            borderRadius: "25px",
                        }}
                        arrowStyle={{
                            display: "none",
                        }}
                    >
                        <div>Compose New Email</div>
                        <br />
                        <NormEdit />
                    </Popup>

                    <Button className="RefreshButton">
                        <i style={{ color: "black" }} class="fas fa-sync"></i>
                    </Button>
                    <Button className="FavButton">
                        <i style={{ color: "black" }} class="fas fa-star"></i>
                    </Button>
                    <Button className="DeleteButton">
                        <i style={{ color: "black" }} class="fas fa-trash-alt"></i>
                    </Button>
                    <Form>
                        <FormGroup>
                            <Label></Label>
                            <Input className="EmailSearch" placeholder="Search Emails..." />
                        </FormGroup>
                    </Form>

                    <Popup
                        trigger={
                            <button className="SettingsButton">
                                <i style={{ color: "black" }} class="fas fa-cog"></i>
                            </button>
                        }
                        position="right center"
                        contentStyle={{
                            backgroundColor: "#465775",
                            width: "98%",
                            height: "80%",
                            color: "white",
                            paddingTop: "25px",
                            borderRadius: "25px",
                        }}
                        arrowStyle={{
                            display: "none",
                        }}
                    >
                        <div>Account Settings</div>
                    </Popup>
                    <Inbox />
                    <div className="IncomingArea">
                        <SingleEmail dataSubject={this.state.data.EmailSubject} dataEmail={this.state.data.EmailBody} From="Testing" Subject="Testing" Date="testing" />
                    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM