cannot get the parent property this property when I have two inner loop

蹲街弑〆低调 提交于 2019-12-02 08:27:58

You can bind the outer map function like

 <div className="row" >
            {

             this.props.stories.map(function(snippet){
                 if(snippet.categ===this.props.categ){
                 return(
                     {snippet.arr.map((item,i)=> <Story key={i} position={i} story={item} ></Story>})


                 );

                 }
             }.bind(this))
            }
        </div>

This will allow you map function to refer to the outer context where prop is available. Also you forgot to include your inner map function inside {}

Other option is to use the arrow function

 <div className="row" >
            {

             this.props.stories.map(snippet) => {
                 if(snippet.categ===this.props.categ){
                 return(
                     {snippet.arr.map((item,i)=> <Story key={i} position={i} story={item} ></Story>})


                 );

                 }
             }.bind(this))
            }
        </div>

Save the this to that before entering the function.

Then use that.props.categ to refer to the outer this.

If that makes any sense :D

Something like so:

render(){
    // place here
    // at the top of render function
    // but outside the return
    var that = this;

    return (
        {something.map(function(snippet){
           if (snippet.categ === that.props.categ){
               // do things here
           }
        })}
    );

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!