JavaScript React Basics (retired) Thinking in Components PropTypes and DefaultProps

Brian Patrick
PRO
Brian Patrick
Pro Student 2,770 Points

When designating propTypes why do we include a coma at the end of the object if there is only one key?

For example: Application.propTypes = { title: React.PropTypes.string, };

2 Answers

Andrew Hickman
Andrew Hickman
Full Stack JavaScript Techdegree Student 10,013 Points

Hey Brian! It's not necessary to type a trailing comma. It's just become conventional, because it's easier to reorder properties and debug if you don't have to worry about you or someone else leaving that comma out. Whatever floats your boat. Do whatever is more convenient for you.

Robert Schaap
Robert Schaap
19,836 Points

Like Andrew mentioned, it isn't necessary but another good reason is when you're using git and someone else is reviewing changes you made to the code in a project.

If your line already ended with a comma and you hit enter to insert a new line, the reviewer will see only 1 change: the line you added. If you also added a comma on the line above, that line will also be shown as changed, though technically it didn't. In bigger files many small changes like that make it hard for a reviewer to see what actually changed and what's just spacing/commas etc.