了解为什么在React类组件中不推荐使用super() [英] Understanding why super() is deprecated in a React class component

查看:1437
本文介绍了了解为什么在React类组件中不推荐使用super()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是React的新手,我正在使用最新版本的React学习关于React组件生命周期的信息。我的超级以下部分代码的调用会标有已弃用的警告标记。我很难理解这一点,因为那里有很多文档仍然使用 super,而且即使从反馈中链接的全文中也无法确定后继者是什么。有任何想法吗?

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);
  }
}

这是警告:

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)


推荐答案

似乎已弃用了可选的context参数,因为它引用了旧版React上下文(v16.3之前的版本)。您使用的是哪个版本的React?

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

我还没有使用React与TypeScript。也许React映射已过期。

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

这篇关于了解为什么在React类组件中不推荐使用super()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆