两种定义React Component的方式有什么不同?

编程入门 行业动态 更新时间:2024-10-26 02:37:26
本文介绍了两种定义React Component的方式有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有两种方法来定义React组件.

There're 2 ways to define a React component.

第一个如下.

class SomeComponent extends React.Component { constructor (props) { super(props) this.state = { someState: false } this._handleOnChangeState = this._handleOnChangeState.bind(this) } _handleOnChangeState (e) { this.setState({ someState: e.target.value }) } .... }

第二个如下.

class SomeComponent extends React.Component { state = { someState: false } _handleOnChangeState = (e) => { this.setState({ someState: e.target.value }) } .... }

这两个代码具有相同的功能,但我想这里有一些不同的东西,例如内存使用情况等等.

These two codes are the same function, but I guess there's some different something like memory usage or etc.

有人可以清楚地说明吗?预先感谢!

Can someone make it clearly? Thanks in advance!

推荐答案

这是ES的新提案(类字段),位于第3阶段.要运行以这种方式编写的代码,您需要像Babel这样的编译器和适当的插件.

This is a new proposal (class fields) for ES which is in stage 3 right now. To run a code written in this way you need a transpiler like Babel and an appropriate plugin.

运输前:

class A { static color = "red"; counter = 0; handleClick = () => { this.counter++; } }

进行转换后(Babel Repl的第二阶段):

After transpile (with stage 2 on Babel Repl):

class A { constructor() { this.counter = 0; this.handleClick = () => { this.counter++; }; } } A.color = "red";

除了官方建议之外, 2ality博客帖子是一个很好的来源看看有什么细节.

In addition to the official proposal 2ality blog post is a good source to see what are the details.

如果您有时间来,这是 reddit帖子阅读讨论风暴,此提案背后的原因是什么:)

Here is a reddit post if you have time to read the discussion storm what is the reason behind this proposal :)

这里的箭头功能是另外一个故事.您可以使用没有构造函数的实例属性,并将代码与标准函数混合.但是,当您想使用类似this的内容时将不起作用:

The arrow function here is a different story. You can use instance properties without constructor and mix your code with standard functions. But when you want to use something like that this won't work:

class App extends React.Component { state = { bar: "baz"} foo() { console.log(this.state.bar) }; render() { return <div><button onClick={this.foo}>Click</button></div>; } }

我们需要以某种方式绑定函数:

We need to bind our function in somehow like:

return <div><button onClick={this.foo.bind(this)}>Click</button></div>

但是,将我们的函数绑定到JSX prop中并不是很好,因为它将在每个渲染器中创建我们的函数.

But, binding our function in a JSX prop is no so good since it will create our function in each render.

执行此操作的一种方法可以很好地绑定到我们的构造函数中:

One way to do this nicely bind in our constructor:

constructor(props) { super(props); this.foo = this.foo.bind( this ); }

但是,如果我必须编写一个构造函数,那有什么意义呢?这就是为什么在我们定义类的地方到处都可以看到箭头功能的原因,就像您的第二个示例一样.借助箭头功能,无需绑定功能.但这与我认为的这项新提议没有直接关系.

But, if I have to write a constructor what is the point? This is why you see arrow functions everywhere where we define the classes like your second example. No need to bind to function thanks to arrow functions. But it is not directly related to this new proposal I think.

更多推荐

两种定义React Component的方式有什么不同?

本文发布于:2023-10-26 21:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531448.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两种   有什么不同   定义   方式   React

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!