【发布时间】: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