javascript - component does not update when react nested loop gets data
女神的闺蜜爱上我
女神的闺蜜爱上我 2017-06-12 09:24:20
0
1
684

Purpose:
According to the found sub-category, find out its parent category and put it into it.

Usage:
Deduplicate the parent category field in the subcategory and put it into par_arr, then traverse par_arr to get the parent category, and then insert the result into the set of the subcategory.

Problem:
The results of console.log(res); include the parent category, but the component is not re-rendered.

It is said on the Internet to use deep copy Object.assign(), I changed
res.data.push(item.data);
to
Object.assign(res.data,item);
is useless.

export function fetch(){
bardata = request('/api/categories').then(res=>{
            //将父分类去重,放入par_arr
            var par_obj={};
            var par_arr=[];
            res.data.map(d=>{
                let _par=d.parent;
                if(!par_obj[_par] && _par!=0){
                    par_obj[_par]=1;
                    par_arr.push(d.parent);
                }
            });

            //根据par_arr获取父分类数据
            Promise.all(par_arr.map(id=>getOne(id))).then(res1=>{
                res1.map(item=>{
                    res.data.push(item.data);
                })
            });
            console.log(res);
            return res;
        })

        return bardata;
}

I changed n methods, checked Baidu n times, and have been stuck with this problem for a week

consult. .

女神的闺蜜爱上我
女神的闺蜜爱上我

reply all(1)
给我你的怀抱

Can this last for a week? ?

To give you some ideas, react updates are either triggered by this.setState or redux dispatch events

Use the ...expand symbol for deep copy, just expand it. Object.assign is not a deep copy in my impression

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template