TSLint and React stateless component naming (PascalCase vs. camelCase)
Asked Answered
S

3

6

Stateless React components should be named in PascalCase, so React can distinguish between native elements and components. Typescripts naming convention dictates that we should use lowerCamelCase or UPPER_CASE for the name of const variables.

How can I satisfy both (React and tslint)?

Explanation

Stagger answered 6/9, 2017 at 6:47 Comment(0)
L
4

I think you have two options here:

  1. Use where appopriate comment like this

    /* tslint:disable-next-line:variable-name */

    to disable tslint warning at that particular line

  2. Use class components instead of functional ones.

Linders answered 6/9, 2017 at 6:55 Comment(3)
Thank you for your answer. Maybe this is another solution: palantir.github.io/tslint/rules/variable-nameStagger
If you will allow both pascal case and camel case - this will effectively kill the purpose of the check. As the most common problem is the mix of the mentioned styles - and tslint is a good tool to prevent this.Linders
Can't we just allow PascalCase for StatelessComponent<S> type?Gil
M
2

You can add the following rule in you tslint.json:

"variable-name": [true, "ban-keywords", "check-format", "allow-pascal-case"]
Maigre answered 4/12, 2018 at 14:18 Comment(0)
P
0

I found a compromise, using a function declaration instead of a variable declaration for my component:

enter image description here

Policyholder answered 27/10, 2018 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.