Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding why super() is deprecated in a React class component

I'm new to React and I'm learning about the React component lifecycle with the latest version of React. My "super" call of the partial code below is flagged with the deprecated warning shown. I've had trouble understanding this one, as a lot of documentation out there still uses "super", and I'm not sure what the successor is, even from the full article linked in the feedback. Any ideas? Thanks.

class App extends Component {
  constructor(props) {
    super(props);
  }
}

Here is the warning:

constructor React.Component<any, any, any>(props: any, context?: any): React:Component<any, any, any> (+1 overload)
@deprecated
@see - https://reactjs.org/docs/legacy-context.html
'(props: any, context?: any): Component<any, any, any>' is deprecated ts(6385)
like image 610
Steven Avatar asked Sep 12 '20 04:09

Steven


People also ask

Is Super props deprecated in React?

props is still available inside render function.

Why do we use super in React class component?

Super(): It is used to call the constructor of its parent class. This is required when we need to access some variables of its parent class. Props: It is a special keyword that is used in react stands for properties. Used for passing data from one component to another.

Why do we need super props in React?

So to ensure that the React. Component 's constructor() function gets called, we call super(props) . super(props) is a reference to the parents constructor() function, that's all it is. We have to add super(props) every single time we define a constructor() function inside a class-based component.

Are class components deprecated React?

Yes, React class components will fade away in the future. If you want to embrace modern React, then you should use function components with hooks. That's why you will find most tutorials out there teaching modern React and no class components anymore.


3 Answers

You need super(props); only if you gonna use this.props in the constructor. Otherwise you can use super(); If you use super(); in the constructor it is not a problem that outside of the constructor you will call this.props. You can read about it in the following link: https://overreacted.io/why-do-we-write-super-props/

class Button extends React.Component {
  constructor(props) {
    super(); //we forgot to pass props
    console.log(props); //{}
    console.log(this.props); //undefined
  }
  // ...
}

It can be even more challenging if this happens in some method that's called from the constructor. And that's why I recommend always passing down super(props), even through it isn't necessary.

class Button extends React.Component {
  constructor(props) {
    super(props); //we passed props
    console.log(props); //{}
    console.log(this.props); //{}
  }
  // ...
}
like image 107
Kiril Dobrev Avatar answered Oct 16 '22 21:10

Kiril Dobrev


super(props); isn't deprecated yet. The deprecation message actually caused from a bug in React's type definition file and is already fixed as of @types/react 16.9.51. Just upgrade the package and you're good to go:

npm install @types/react
like image 39
weeix Avatar answered Oct 16 '22 22:10

weeix


It looks like the optional context parameter is deprecated because it refers to the legacy React context (pre v16.3). What version of React are you using?

https://reactjs.org/docs/legacy-context.html

I have not used React with TypeScript. Maybe React mappings are out of date.

like image 39
Dmitry S. Avatar answered Oct 16 '22 20:10

Dmitry S.