【问题标题】:passing props to componentDidMount将道具传递给 componentDidMount
【发布时间】:2019-04-29 13:29:13
【问题描述】:

能够在我的仪表板组件中呈现道具,无法将其传递给 componentDidMount 以进行进一步的 ajax 处理。

渲染工作正常并显示信息。

我正在使用 firebase,并且工作正常。

App.js

// React core.
import React, { Component } from 'react';
import Dashboard from './components/Dashboard';

class App extends Component {

 //firebase...

  state = {
    isSignedIn: undefined,
    displayName: undefined,
    email: undefined
  };

  componentDidMount() {

      this.setState({providerData: user.providerData}) 
      this.setState({ displayName: user.providerData[0].displayName })  
      this.setState({ email: user.providerData[0].email })   
    });
  }

  render() {
    return (

      <div className={styles.container}>
        {this.state.isSignedIn !== undefined && !this.state.isSignedIn &&
          //Not signed-in yet
          <div>

            <Login />

          </div>
        }

        {this.state.isSignedIn &&
          //Signed in
          <div>
            <Dashboard 
              displayName={this.state.displayName} 
              email={this.state.email} />

          </div>
        }   
      </div>
    );
  }
}

export default App;

仪表板.js

import React, { Component } from 'react';


class Dashboard extends Component {


    componentDidMount() {
        var thisComponent = this
        console.log(thisComponent.props.email)
    }

    render()  {
        const email = this.props.email

        return (
            <div>
                <h1>Hello, {this.props.displayName}!</h1>
                <h2>Your email is: {email}</h2> 
            </div>
        );
    }
}

export default Dashboard;

Dashboard.js 中的 componentDidMount 让我很头疼。无法为进一步的 ajax 进程发送电子邮件值。 应该在控制台中收到电子邮件,相反,我得到“未定义”。

【问题讨论】:

  • 你确定在第一次挂载时将email 属性传递给这个组件吗?
  • 你能分享你是如何传递道具的例子:-
  • 是的,它确实按预期显示在

    标记中...但无法在 componentDidMount 中再次引用

  • 请显示您收到电子邮件的代码并在道具中传递电子邮件。用目前的代码,不可能解决你的问题
  • 创建一个构造函数和super(props)

标签: reactjs react-props


【解决方案1】:

唯一可能发生在您身上的情况是email 来自某个异步调用。事情就是这样发生的:

  1. emailundefined 状态。但是您进行了异步调用以获取 email
  2. 您渲染组件,但 emailundefined,因为异步调用尚未完成,因此您在 componentDidMount 中控制台 undefined
  3. 然后你从异步调用中得到结果,setState 的电子邮件,它归结为道具,并使用正确的电子邮件重新呈现 Dashboard

这样,您会看到呈现的电子邮件,但在 componentDidMount 中它是未定义的。它渲染了 2 次,并且仅在几秒钟内,在组件已经挂载后,您将获得正确的 email

这是您可以看到数据的唯一情况,但它在 componentDidMount 中未定义,我几乎可以肯定这是您的情况。

您几乎没有提供任何代码来解决您的问题,而我唯一能做的就是通过对您的案例进行一些假设来告诉您问题的原因。希望这可以帮助您了解您的问题并解决它。

编辑:在您的代码中,您使用firebase(异步调用)收到电子邮件,所以我的假设是正确的,现在只需要知道预期的结果是什么。

编辑: 如果您需要对组件内的电子邮件进行一些操作,请使用componentDidMount

对于你的情况,你可以这样做

componentDidUpdate(prevProps) {
  // Typical usage (don't forget to compare props):
  if (this.props.email != undefined && this.props.email !== prevProps.email) {
    // Do what you want with email
  }
}

【讨论】:

  • 这是有道理的,我将如何在异步渲染后传递值? (抱歉还没有咖啡:/)
  • would do I pass the value after the async render 是什么意思?我无法理解你的问题。
  • 修正了第一条评论的语法
  • 你想对Dashboard中的email做什么?您可以在componentDidUpdate
  • 我最终需要在 componentDidMount 中使用 email 值进行 axios 调用。
猜你喜欢
  • 2019-11-30
  • 1970-01-01
  • 2017-10-03
  • 2018-03-26
  • 1970-01-01
  • 2020-08-27
  • 2021-09-27
  • 2014-03-12
  • 2018-07-07
相关资源
最近更新 更多