简体   繁体   English

类/功能组件,内部方法

[英]Class/Functional component, methods within

I'm quite new to ReactJs and I'm trying to understand a little more detail.我对 ReactJs 很陌生,我试图了解更多细节。

In a Class component, these are the two ways that I know I can declare a handler method to change the state在 Class 组件中,这是我知道的两种方法,我可以声明一个处理程序方法来更改 state

 classChangeState=()=>{
         this.setState({
             Person:[
                 {firstName:"test", secondName:55}
             ]
         })
     }

classChangeState2(){
    console.log("Inside Change state click")
    this.setState({
         Person:[
             {firstName:"test", secondName:55}
         ]
     })enter code here
 //classChangeState2 require me to bind "this" inside render method

In functional component the below two ways I can do it在功能组件中,我可以通过以下两种方式做到这一点

    function changeStateClick(){
            changeState({
             Person:[
                {firstName: "Just aasefaesfstring property", secondName: 32}
            ]
        })
        }

    const changeStateClick2 =()=> changeState({
             Person:[
                {firstName: "Just a[[[string property", secondName: 32}
            ]
        })

I have a few questions 1) how does React know that classChangeState2 is a method without the "function"?我有几个问题 1) React 怎么知道 classChangeState2 是一个没有“函数”的方法?

2) I know that I can pass in a newName as parameters in all of the above methods above but I must bind "THIS" in the render for all methods. 2) 我知道我可以在上述所有方法中传入一个 newName 作为参数,但我必须在所有方法的渲染中绑定“THIS”。 For eg methodName.bind(this,"newNamehere")例如 methodName.bind(this,"newNamehere")

why is this?为什么是这样? Even for the functional component where initially I did not need to bind when I want to add "newName" as a parameter, I must now bind.即使对于最初我想添加“newName”作为参数时不需要绑定的功能组件,我现在也必须绑定。 Can someone explain, please?有人可以解释一下吗?

classChangeState=(newName)=>{
         this.setState({
             Person:[
                 {firstName:newName, secondName:55}
             ]
         })
     }

I have a few questions 1) how does React know that classChangeState2 is a method without the "function"?我有几个问题 1) React 怎么知道 classChangeState2 是一个没有“函数”的方法?

It's not related to React , but ES6.它与React无关,而是与 ES6 相关。 Classes are syntactical sugar and they are just special functions.类是语法糖,它们只是特殊的功能。 And what you're seeing as a method is just a short hand for a function assigned to a method's name.您所看到的方法只是分配给方法名称的 function 的简写。 So when you write this:所以当你写这个时:

   class fooClass {
    bar(...) {} 
   }

fooClass is actually a function and the methods inside that for eg bar are written to fooClass.prototype . fooClass实际上是一个 function 并且其中的方法例如bar被写入fooClass.prototype Also, you'd like to see,另外,你想看看,

Starting with ECMAScript 2015, a shorter syntax for method definitions on objects initializers is introduced.从 ECMAScript 2015 开始,引入了一种更短的对象初始化方法定义语法。 It is a shorthand for a function assigned to the method's name.它是分配给方法名称的 function 的简写。

 const obj = {
  foo() {
    return 'bar';
  }
};

console.log(obj.foo());

You can go through more on MDN- Classes and function-definitions MDN您可以通过 MDN 上的更多内容来go-类函数定义 MDN

Coming to the second part of the question,来到问题的第二部分,

2) I know that I can pass in a newName as parameters in all of the above methods above but I must bind "THIS" in the render for all methods. 2) 我知道我可以在上述所有方法中传入一个 newName 作为参数,但我必须在所有方法的渲染中绑定“THIS”。 For eg methodName.bind(this,"newNamehere")例如 methodName.bind(this,"newNamehere")

This syntax is experimental class property and you can use this instead of bind ing the method in the constructor.此语法是实验性的 class 属性,您可以使用它而不是在构造函数中bind方法。 Note that this syntax may change.请注意,此语法可能会更改。

Read More https://reactjs.org/docs/react-without-es6.html#autobinding阅读更多https://reactjs.org/docs/react-without-es6.html#autobinding

https://babeljs.io/docs/en/babel-plugin-transform-class-properties/ https://babeljs.io/docs/en/babel-plugin-transform-class-properties/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM