-1

I have a class component in a reactjs project. The following code segment works fine. But inside the map function, I am not able to access the state variables. I am getting the below error,

TypeError: Cannot read properties of undefined (reading 'state')

Even console.log(this.state.total); didn't work inside the map function.

constructor(props) {
    super(props);
    this.state = {
        page: 2,
        total: 0,
    };
}

<Pagination>
{this.state.total > 0 ?
    Array(Math.ceil(this.state.total / 10)).fill(0).map(function (object, i) {
        
        return <Pagination.Item active={false} key={i}>{i + 1}</Pagination.Item>
    }).bind(this) : null}
</Pagination>
George Raveen
  • 413
  • 1
  • 5
  • 10

1 Answers1

1

Change the anonymous function in your map to an arrow function like this. Change from this:

map(function () {
  console.log(this.state)
  return ...
})

to this:

map(() => {
  console.log(this.state)
  return ...
})
Youssouf Oumar
  • 29,373
  • 11
  • 46
  • 65
tony
  • 466
  • 6
  • 22