【发布时间】:2019-12-23 04:38:19
【问题描述】:
我有三个独立的useEffect 函数。一种是获取提交记录:
useEffect(() => {
if (props.loading_submissions != true) {
props.fetchSubmissionsByReviewRound(reviewRoundId);
}
}, [reviewRoundId])
另一个是用于获取学生。
useEffect(() => {
if (props.loading_students != true) {
props.fetchStudentsByCourse(courseId, reviewRoundId);
}
}, [reviewRoundId])
我更喜欢先获取Students,一旦它们被加载并保存为redux状态,就应该获取提交。我想知道我怎么能做到这一点。任何帮助表示赞赏。
下面是mapStateToProps 和mapDispatchToProps 方法:
const mapStateToProps = (state) => ({
submissions: state.submissionReducer.submissions,
students: state.studentReducer.students,
loading_students: state.studentReducer.loading,
loading_submissions: state.submissionReducer.loading,
error: state.studentReducer.error,
})
const mapDispatchToProps = (dispatch) => {
return {
fetchSubmissionsByReviewRound: (reviewRoundId) => dispatch(FetchSubmissionsByReviewRound(reviewRoundId)),
fetchStudentsByCourse: (courseId, reviewRoundId) => dispatch(FetchStudentsByCourse(courseId, reviewRoundId)),
}
}
我正在使用axios 获取数据:
export function FetchStudentsByCourse(courseId, reviewRoundId) {
return dispatch => {
dispatch(studentsDataOperationBegin());
axios.get("api/Student/FetchStudentsByCourse", { params: { courseId, reviewRoundId } })
.then(response => {
console.log('Students in the course are fetched by course id.');
const students = new schema.Entity('students');
const normalizedData = normalize(response.data, [students]);
dispatch(fetchStudentsSuccess(normalizedData.entities.students))
})
.catch(error => { studentsDataOperationFailure(error) });
}
}
export function FetchSubmissionsByReviewRound(reviewRoundId) {
return dispatch => {
dispatch(submissionDataOperationBegin());
axios.get('api/Submission/FetchSubmissionsByReviewRound', { params: { reviewRoundId } })
.then(response => {
console.log('Submissions are fetched.');
const submissions = new schema.Entity('submissions');
const normalizedData = normalize(response.data, [submissions]);
dispatch(fetchSubmissionsByReviewRoundSuccess(normalizedData.entities.submissions))
})
.catch(error => { submissionDataOperationFailure(error) });
}
}
【问题讨论】:
标签: ajax reactjs redux axios react-hooks