Why is getInitialState not being called for my React class?

后端 未结 3 1230
无人共我
无人共我 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 16:11

    To expand a bit on what it means

    getDefaultProps and propTypes are really just properties on the constructor.

    the "on the constructor" bit is weird wording. In normal OOP language it just means they are "static class variables"

    class MyClass extends React.Component {
        static defaultProps = { yada: "yada" }
        ...
    }
    

    or

    MyClass.defaultProps = { yada: "yada" }
    

    you can also refer to them within the class like:

    constructor(props) {
        this.state = MyClass.defaultProps;
    }
    

    or with anything you declare as a static class variable. I don't know why this is not talked about anywhere online with regards to ES6 classes :?

    see the docs.

提交回复
热议问题