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

Invalid react/no-direct-mutation-state in constructor #1382

Closed
emmanuelgautier opened this issue Aug 18, 2017 · 5 comments
Closed

Invalid react/no-direct-mutation-state in constructor #1382

emmanuelgautier opened this issue Aug 18, 2017 · 5 comments

Comments

@emmanuelgautier
Copy link

emmanuelgautier commented Aug 18, 2017

In the following special case, the react/no-direct-mutation-state rule throw an invalid error

class OneComponent extends Component {
  constructor() {
    super();

    class AnotherComponent extends Component {
      constructor() {
        super();
      }
    }

    this.state = {};
  }
}

A better way is to not declare a component into another component constructor but that is an invalid linter error .

@ljharb
Copy link
Member

ljharb commented Aug 19, 2017

What error is thrown?

@emmanuelgautier
Copy link
Author

Error thrown : Do not mutate state directly. Use setState() react/no-direct-mutation-state

@ljharb
Copy link
Member

ljharb commented Aug 19, 2017

This seems like a bug - although I can't conceive of why you'd want to create a component in the constructor like that.

@emmanuelgautier
Copy link
Author

emmanuelgautier commented Aug 20, 2017

I agree with you, this code is not mine. It should be a closure problem to have access into the AnotherComponent to some variables declared in the first constructor

@ma-alexiou
Copy link

I know I am late to the party, but you have made a typo at the constructor of OneComponent class. Changing contructor to constructor removes the eslint warning.

In your case eslint treats contructor as it would with any other method of OneComponent's class and it warns you that you should not mutate the state this way and use the setState method.

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

No branches or pull requests

3 participants