Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am confused about the lifecycle sequence in 16.3 #1792

Closed
stunstunstun opened this issue Apr 26, 2018 · 3 comments
Closed

I am confused about the lifecycle sequence in 16.3 #1792

stunstunstun opened this issue Apr 26, 2018 · 3 comments

Comments

@stunstunstun
Copy link

stunstunstun commented Apr 26, 2018

I defined the rules through the issues below which are confusing to me.

class Textarea extends Component {
  constructor(props) { // lint error!: constructor should be after getDerivedStateFromProps
    super(props)
    this.state = {
      count: 0,
    }
    this.handleChange = this.handleChange.bind(this)
  }

  static getDerivedStateFromProps(nextProps, prevState) {
  ...
  }
...
}

Of course I know it is because of static keyword. How should I change the current rule below?

.eslintrc

"react/sort-comp": ["warn", {
      "order": [
        "static-methods",
        "lifecycle",
        "everything-else",
        "render"
      ],
      "groups": {
        "lifecycle": [
          "displayName",
          "propTypes",
          "contextTypes",
          "childContextTypes",
          "mixins",
          "statics",
          "defaultProps",
          "constructor",
          "instance-variables",
          "getDefaultProps",
          "state",
          "getInitialState",
          "getChildContext",
          "getDerivedStateFromProps",
          "componentWillMount",
          "UNSAFE_componentWillMount",
          "componentDidMount",
          "componentWillReceiveProps",
          "UNSAFE_componentWillReceiveProps",
          "shouldComponentUpdate",
          "componentWillUpdate",
          "UNSAFE_componentWillUpdate",
          "getSnapshotBeforeUpdate",
          "componentDidUpdate",
          "componentDidCatch",
          "componentWillUnmount"
        ]
      }
    }]
@ljharb
Copy link
Collaborator

ljharb commented Apr 26, 2018

The React 16.3 additions haven’t yet been added to eslint-plugin-react; see jsx-eslint/eslint-plugin-react#1767

As far as it’s concerned at the moment, getDerivedStateFromProps is just an arbitrary method that starts with “get”.

@ljharb
Copy link
Collaborator

ljharb commented Apr 26, 2018

Specifically tho, it’s becsuse you have “statics” before “constructor”.

@stunstunstun
Copy link
Author

@ljharb Thanks, but It has nothing to do with the statics of the lifecycle array. I just want to use one of the lifecycle functions w/static getDerivedStateFromProps(nextProps, prevState) and I just don't want to delete the keyword static-method from the order array below.

As-is

As shown above, a lint error occurred.

order: [
    'static-method',
    'lifecycle',
    'everything-else',
    'render'
  ],

To-be

Of course this works well.

order: [
    'lifecycle',
    'everything-else',
    'render'
  ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants