简体   繁体   English

react js无法在setState回调function中获取更新值

[英]react js cannot get updated value in setState callback function

createSyllabusObject = () => {
        let number = 0;
        let syllabusTemp = [];
        let { lessonsList, exportedSyllabus } = this.state;
        Promise.all(lessonsList.map(async (lesson) => {
            let name = null;
            await getTeacherById(lesson.teacher).then(res => {
                name = res.data.name
                number += 1;
            })
            syllabusTemp.push({
                No: number,
                title: lesson.title,
                description: lesson.description,
                teacher: name,
                Time: moment(lesson.commenceDate).format('DD/MM/YYYY HH:mm')
            })
        })).then(() => {
            console.log("temp", syllabusTemp)
            this.setState(
                {
                    exportedSyllabus: syllabusTemp
                }
                , () => {
                    console.log("now", exportedSyllabus)
                })
        })
    }

This function will be called when I click a button.当我单击按钮时,将调用此 function。

I cannot get the updated state value in console.log("now", exportedSyllabus) when first time I click the button, but if I click it more than 1 time, all other results will be correct.第一次单击按钮时,我无法在console.log("now", exportedSyllabus)中获取更新的 state 值,但如果单击超过 1 次,则所有其他结果都是正确的。

The result of console.log("temp", syllabusTemp) is always right no matter how many times I click the button.无论我点击多少次按钮, console.log("temp", syllabusTemp)的结果总是正确的。

I really confused about this, why I cannot get a proper output of console.log("now", exportedSyllabus) when I click the button first time.我对此感到非常困惑,为什么当我第一次单击该按钮时,我无法获得console.log("now", exportedSyllabus)的正确 output 。

You'll want to access the current state in the setState callback, not the previous state saved in the createSyllabusObject enclosure.您需要在setState回调中访问当前的 state,而不是之前保存在createSyllabusObject附件中的 state。

createSyllabusObject = () => {
    let number = 0;
    let syllabusTemp = [];
    let { lessonsList, exportedSyllabus } = this.state;
    Promise.all(lessonsList.map(async (lesson) => {
        let name = null;
        await getTeacherById(lesson.teacher).then(res => {
            name = res.data.name
            number += 1;
        })
        syllabusTemp.push({
            No: number,
            title: lesson.title,
            description: lesson.description,
            teacher: name,
            Time: moment(lesson.commenceDate).format('DD/MM/YYYY HH:mm')
        })
    })).then(() => {
        console.log("temp", syllabusTemp)
        this.setState(
            {
                exportedSyllabus: syllabusTemp
            }
            , () => {
                console.log("now", this.state.exportedSyllabus) // <-- access this.state!!
            })
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM