Why is getInitialState not being called for my React class?

后端 未结 3 1270
无人共我
无人共我 2021-02-01 15:20

I\'m using ES6 classes with Babel. I have a React component that looks like this:

import { Component } from \'react\';

export default class MyReactComponent ext         


        
3条回答
  •  甜味超标
    2021-02-01 15:46

    The developers talk about ES6 class support in the Release Notes for v0.13.0. If you use an ES6 class that extends React.Component, then you should use a constructor() instead of getInitialState:

    The API is mostly what you would expect, with the exception of getInitialState. We figured that the idiomatic way to specify class state is to just use a simple instance property. Likewise getDefaultProps and propTypes are really just properties on the constructor.

提交回复
热议问题