私は、componentDidMount()
に関する情報を取得するためのAPI取得要求を必要とするコンポーネントを持っています。そのリクエストは、私がComponentWillMount()
にある関数を介して設定している状態で保存された情報に依存します。WillMount()に設定された状態はDidMount()にアクセスできない
私willMount()
ランは、その後、私は私のrender()
方法を打つが、状態が設定されていない、ので、それはdidMount()
を打つが、データがまだ状態でないため失敗します。どこが間違っていますか?
編集:すべての関数がコンストラクタにバインドされている(図示せず)
componentWillMount() {
this.enforceEmployeeAuth();
this.loadEmployeeInfo();
}
componentDidMount() {
this.fetchSurveyFields();
}
// Initial check to confirm if the user should be allowed to access any information.
enforceEmployeeAuth() {
// Get request to confirm that the current user is of type EMPLOYEE
API.get("user/employee_auth", {}, (res) => {
// Employee Auth: True if employee, else false
this.setState({
employee_auth: res.employee_auth,
});
});
}
// Load up relevant information for currentUser/employee
loadEmployeeInfo() {
API.get("client/currentEmployee", {}, function(res) {
this.setState({
employee : res.employee,
employee_company : res.employee_company,
})
}.bind(this));
}
fetchSurveyFields() {
debugger
API.get('client/survey', {
survey: this.state.employee_company.survey_name
}, function(res) {
debugger
})
}
render() {
debugger
return (
<h2 className="text-charcoal text-left">Employee Rubric</h2>
)
}
と同じように、あなたはuが呼び出されたときの状態を正しくloadEmployeeInfo()メソッド内のデータを使用して設定されているかどうかを確認しました – Cruiser
データを使用する前に.getの結果が戻ってくるのを待つ必要があるとしていますfrom componentWillMount()method –
私はデバッガでフローをチェックしました。 'willMount()' - >関数を実行します.... 'render()' - >状態更新なし... 'didMount()' - >状態はまだ変更されていません関数が失敗します.... 'render()' - >最初の状態変更がレンダリングされます.... 'render()' - > 2番目の状態変更がレンダリングされます –