Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correct naming specification for react components

I am trying to find the best naming convention for components in a React.js app. This is how I have currently been doing it...

Imagine I have a searchBar component that I want to render in my table component.

search-bar.js

var React = require('react');

var SearchBar = React.createClass({
  /* code for search component */
});

module.exports = SearchBar;    

table.js

var React = require('react');
var SearchBar = require('search-bar');

var Table = React.createClass({
    render: function() {
        return (
                <SearchBar />
        );
    }
});
module.exports = Table;

Is this naming convention okay or is it standard?

  • hyphen-delimited for component file names
  • PascalCase for the component function declarations.
like image 773
svnm Avatar asked Mar 20 '15 00:03

svnm


People also ask

How should React components be named?

Allow two components with the same name - Components should have declarative and unique names in the application, to avoid confusion about the responsibility of each one. However, the above approach opens a breach to having two components with the same name, one being a container and other being a presentational.

Should React component filenames be capitalized?

And React components must be uppercased In JSX, so an uppercase filename keeps the component name and filename in sync.

Should React components be camelCase?

TitleCase for component files is best because it allows you to know other camelCase files are exporting something else than a component. React is not opinionated on this. Use what works best for you.


1 Answers

In a general sense it seems that react is young enough that there aren't yet many particularly strong style conventions, but what you've outlined is reflective of what I've seen for the most part. The react docs all PascalCase their component names and if there is in fact a style convention for module file names hyphen delimited seems far and above the most common.

like image 137
aaronjkrause Avatar answered Oct 22 '22 23:10

aaronjkrause