whitebear
whitebear

Reputation: 12423

Using class member not state

I want to make class member file_name

I don't want to use this.state because I don't want to render when file_name is changed.

class ListMix extends React.Component {
  constructor(props) {
    this.file_name = "";// it shows the error. 
                        //TypeError: Cannot set property 'file_name' of undefined
    super(props);
    this.state = {
      result :[]
    }
  }
  render(){
     return();
  }
}

Upvotes: 1

Views: 48

Answers (2)

allakonda harish
allakonda harish

Reputation: 70

The basic/simple understanding of OOPS concept.

When you extend a class with another, and if you use constructor, then you have to use super function to inherit the extended component's properties first. The rule is inherit the properties from the extended component then build your main component. Otherwise you will get an error.

Upvotes: 1

atomrc
atomrc

Reputation: 2583

That's a simple one, you just need to call super(props) before adding the file_name property :)

 constructor(props) {
   super(props);
   this.file_name = "";
}

Upvotes: 1

Related Questions