search

Home  >  Q&A  >  body text

javascript - component does not update when react nested loop gets data

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. .

女神的闺蜜爱上我女神的闺蜜爱上我2712 days ago672

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-06-12 09:26:20

    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

    reply
    0
  • Cancelreply