0
votes

I am using React, Material-UI and lodash but the following code within the render method gives me an error saying "TypeError: Cannot read property 'selectPage' of undefined" :

        tmppage = _.map(_.range(0, this.state.groupsdt.length), function (i) {

                    return (
                            <MenuItem >
                                <IconButton onClick={() => { {this.selectPage(i)} }}><WebAsset /></IconButton>
                                Page {i + 1}
                            </MenuItem>
                        )
        });

I've defined this.selectPage in constructor(props) as follows :

constructor(props){
    super(props);
    this.state = {
                    loading: true,
                    modalopen: false,
                    menuopen: false,
                    urls:null,
                    groupsdt:null,
                    webkey:null,

                    currentpage:0,

                    currentBreakpoint: 'lg',
                    mounted: false,
                    layouts: {lg: null},

                    opentopublic:false,

                  };

    this.addPage      = this.addPage.bind(this);
    this.selectPage   = this.selectPage.bind(this);
    this.addBox       = this.addBox.bind(this);
    this.removeBox    = this.removeBox.bind(this);
    this.doPost       = this.doPost.bind(this);

    this.doHome       = this.doHome.bind(this);
}

Please tell me what I'm missing... and thank you in advance.

2

2 Answers

0
votes

Your problem seems to be coming from using function keyword in callback for map, try using arrow function instead. You can read more on arrow functions here.

_.map(_.range(0, this.state.groupsdt.length), (i)=> {
    ....
});
0
votes

Above answer is very right - You should change the callback inside your map to an arrow function that would make this callback bound to its lexical scope which is your class and this will have the selectPage property