20

Is there an agreed-upon convention for indentation in React JSX? 2 spaces, 4 spaces, tabs, etc.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
  • 1
    Because of the large use of callbacks, many style guides suggest to use `2 spaces` for indentation, but this question regards javascript in general, not react. – Hitmands Apr 28 '16 at 10:01
  • Okay, I agree. Good to know. –  Apr 28 '16 at 10:02
  • A bit late to the party. Anyway, I think 2 spaces hides the problem. If you have many callbacks, you should refactor your code instead of adding another layer of indentation. – David Apr 29 '19 at 07:24

4 Answers4

22

You should just use the same indentation as the rest of your javascript code.

That said react ecosystem seems to use mostly double spaces so I'd use that for consistency.

silkAdmin
  • 4,640
  • 10
  • 52
  • 83
3

Most popular libraries tend to use 2 spaces, but you can change this if you and your team agree on a standard.

You can setup up eslint so that your code base is standardised. I would definitely recommend you to take a look at one of the most popular javascript style guide by Airbnb.

Max
  • 651
  • 8
  • 16
2

You should use the identation style that fit your style. You can use something like ESlint to define a convention. 4 or 2 spaces or tabs, is just a style.

severin.julien
  • 1,314
  • 15
  • 27
0

The react source code and sample code in the React tutorial use 2 spaces. Also it seems like many of the Github projects related to React use 2.

James Lawruk
  • 30,112
  • 19
  • 130
  • 137