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?
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.
And React components must be uppercased In JSX, so an uppercase filename keeps the component name and filename in sync.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With