在我componentDidMount()进行API调用以获取一些数据时,此调用然后设置我在渲染中使用的状态对象.
componentDidMount() {
const { actions } = this.props;
this.increase = this.increase.bind(this);
// api call from the saga
actions.surveyAnswersRequest();
// set breadcrumb
actions.setBreadcrumb([{ title: 'Score' }]);
actions.setTitle('Score');
this.increase();
}
Run Code Online (Sandbox Code Playgroud)
在我的渲染函数中,我将一些prop值传递给视图文件:
render() {
const { global, gallery, survey_answers, survey, survey_actual_answers } = this.props;
if (global.isFetching) {
return <Loading />;
}
return this.view({ gallery, survey_answers, survey, survey_actual_answers });
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是survey_actual_answers第一次加载页面时没有设置prop,但是当我刷新页面时,prop返回数据正常,脚本的其余部分将运行.这是它第一次为该prop值返回一个空数组.
这就是我传递道具的方式:
Score.propTypes = {
actions: PropTypes.object.isRequired,
global: PropTypes.object.isRequired,
survey: PropTypes.object.isRequired,
survey_answers: PropTypes.object.isRequired,
gallery: PropTypes.object.isRequired,
survey_actual_answers: PropTypes.array.isRequired,
survey_score_system: PropTypes.array.isRequired, …Run Code Online (Sandbox Code Playgroud)