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

Add a rule to require $ReadOnly Props and State for React components #397

Closed
kangax opened this issue Apr 25, 2019 · 3 comments · Fixed by #400
Closed

Add a rule to require $ReadOnly Props and State for React components #397

kangax opened this issue Apr 25, 2019 · 3 comments · Fixed by #400
Labels

Comments

@kangax
Copy link
Contributor

kangax commented Apr 25, 2019

It's a good practice to annotate React' Props/State types as $ReadOnly, preventing mutation.

Props can not be mutated in React and doing so could lead to unexpected behavior. State can be mutated but is recommended against, since it's not safe with multiple consecutive calls. There's a lint rule to catch the latter but it's very rudimental and doesn't handle all cases.

type Props = $ReadOnly<{||}>
type State = $ReadOnly<{||}>

class Foo extends Component<Props, State> { ... }

I don't know if adding React-specific rules is OK in this plugin, but I wanted to throw this idea before making a custom lint rule in our codebase.

@gajus
Copy link
Owner

gajus commented Apr 26, 2019

I think given that there is a big overlap between the React and Flow user-base, this is a reasonable request. Perhaps it is a better fit for https://github.com/yannickcr/eslint-plugin-react though?

@kangax
Copy link
Contributor Author

kangax commented Apr 29, 2019

@gajus I guess it could also be in eslint-plugin-react. It doesn't have any Flow-related rules at the moment and eslint-plugin-flowtype doesn't have any React-related rules at the moment :)

I'll probably just make a PR here.

@gajus
Copy link
Owner

gajus commented May 15, 2019

🎉 This issue has been resolved in version 3.9.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gajus gajus added the released label May 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants