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

[react/destructuring-assignment] Shows weird behaviour when I declare a variable with the name context #2309

Closed
roman-16 opened this issue Jun 13, 2019 · 6 comments · Fixed by #2797

Comments

@roman-16
Copy link

In a functional component I get an error when I declare a variable with the name context.
Example code:

const Test = props => {
  const context = useContext(someContext);

  return (
    <div {...props}>
      {context.test} // Here message: 'Must use destructuring context'
    </div>
  );
}
@ljharb
Copy link
Member

ljharb commented Jun 13, 2019

The error here should appear imo with any variable name coming from the useContext hook. Does the error disappear if you use a different variable name?

@roman-16
Copy link
Author

No, then it doesn't appear.

@ljharb
Copy link
Member

ljharb commented Jun 14, 2019

There seems like two bugs then:

  1. the variable name context shouldn't be special
  2. the return of useContext should be caught by this rule with every var name (altho this might need an option)

@ed-jinyoung-park
Copy link
Contributor

I think the error message on that case is correct following this rule, isn't it?
can you explain more why there is a bug ?

@ljharb
Copy link
Member

ljharb commented Aug 15, 2020

@Zinyon the rule is supposed to enforce legacy context being destructured; it's not supposed to warn on useContext, or on any variable that happens to be named context.

@ed-jinyoung-park
Copy link
Contributor

@ljharb Thank you, it makes sense! and I'll try fix this.

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