What is the difference between using constructor vs getInitialState in React / React Native?

后端 未结 6 843
慢半拍i
慢半拍i 2020-11-28 17:24

I\'ve seen both used interchangeably.

What are the main use cases for both? Are there advantages / disadvantages? Is one a better practice?

6条回答
  •  温柔的废话
    2020-11-28 17:56

    The big difference is start from where they are coming from, so constructor is the constructor of your class in JavaScript, on the other side, getInitialState is part of the lifecycle of React . The constructor method is a special method for creating and initializing an object created with a class.

提交回复
热议问题