【问题标题】:Fetching data just after another fetch is complete in react hooks在反应钩子中完成另一个提取后立即提取数据
【发布时间】: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状态,就应该获取提交。我想知道我怎么能做到这一点。任何帮助表示赞赏。

下面是mapStateToPropsmapDispatchToProps 方法:

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


    【解决方案1】:

    如果您希望其中一个依赖于另一个,那么我将在一个 useEffect() 方法下加入它们,并从它们中生成 promise 函数。完成后, async/await 将成为您的朋友。

    但是,如果您希望将来自 Students 调用的数据加载给用户,然后浏览器去获取另一个,那么我将使用第二个 setState 来监视由第一的。比如这样:

    const [studentDataReceived, setStudentDataReceived] = useState(false);
    
    useEffect(() => {
        if (props.loading_students != true) {
           props.fetchStudentsByCourse(courseId, reviewRoundId);
        }
        setStudentDataReceived(true)
    }, [reviewRoundId])
    
    useEffect(() => {
      if(studentDataReceived){
        if (props.loading_submissions != true) {
            props.fetchSubmissionsByReviewRound(reviewRoundId);
        }
      }
    }, [studentDataReceived])
    

    【讨论】:

    • 谢谢!如果用 async/await 实现,功能上有什么区别?
    • 在获取学生数据之前,此代码将studentDataReceived 设置为true
    • 然后setStudentDataReceived 在实际发生提取的父元素中。这样,您可以在实际收到时将其设置为已收到,并在发生错误时加载错误。
    猜你喜欢
    • 2022-10-04
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2018-07-14
    • 2019-11-02
    • 1970-01-01
    相关资源
    最近更新 更多