There are some basic rules you have to consider. We think this is a very handsome way to work with React and chayns®.
- Only one React component per file.
- But multiple Stateless or Pure components are allowed in one file.
- Use JSX syntax when writing React component.
- Project structure
- Naming
- PropType
- Component
- Properties
- Methods
- Spacing
- Alignment
- Quotes
- Parentheses
- Tag
- Refs
- chayns®
- Source
- This is a possible structure for a project with
ReactandRedux.
..
src
|-actions
|-todo.js
|-components
|-header
|-headline
|-Headline.jsx
|-headline.scss
|-intro
|-Intro.jsx
|-intro.scss
|-todos
|-Todos.jsx
|-todos.scss
|-todo
|-Todo.jsx
|-TodoContainer.js
|-todo.scss
|-add-todo
|-AddTodo.jsx
|-AddTodoContainer.js
|-AddTodo.scss
App.jsx
App.scss
|-reducers
|-todo.js
|-index.jsx
|-index.html
...
- Filename: Use PascalCase for filenames
ChaynsUser.jsx - ReferenceNaming: PascalCase for React components, camelCase for their instances.
import ChaynsUser from './ChaynsUser';
const chaynsUser = <ChaynsUser />;- ComponentNaming: Use the filename as the component name. You can use a
index.jsxif the directory name has the same name as the component. Better rename the directory.
import ChaynsUser from './ChaynsUser';- Properties Avoid using DOM component prop names for different purposes e.g. don't use "style" or "className" as prop names.
import SchoolClass from './SchoolClass';
<SchoolClass schoolClassName ="Science-Laboratory" />- PropTypes will help you to keep your code clean and structured.
- Always define defaultProps for all non-required props.
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
const propTypes ={
userId: PropTypes.number.isRequired,
firstName: PropTypes.string.isRequired,
nameAffix: PropTypes.string
};
const defaultProps= {
nameAffix: 'Hello'
};
class User extends PureComponent{
render(){
return(
<div id={this.props.userId}>
{this.props.nameAffix + this.props.firstName}
</div>
)
}
}
User.propTypes = propTypes;
User.defaultProps = defaultProps;
export default User- This can be used with the plugin
transform-class-properties
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
export default class User extends PureComponent{
static propTypes ={
userId: PropTypes.number.isRequired,
firstName: PropTypes.string.isRequired,
greeting: PropTypes.string,
onChange: PropTypes.func.isRequired
};
static defaultProps= {
greeting: 'Hello, '
};
render(){
return(
<div id={this.props.userId}>
{this.props.nameAffix + this.props.firstName}
</div>
)
}
}- Declare your React component with
extends PureComponent. - We use PureComponent instead of Component because it handles the shouldComponentUpdate method for us.
- If we want to define our own shouldComponentUpdate logic we have to use Component instead of PureComponent.
- Keep your render function short and clean.
- You can use stateless components to minimize your render function. Especially if you can use the stateless components multiple times.
export default class Tapp extends PureComponent {
render(){
return(
<div className="tapp">
<div className="tapp__intro">
<h1>My Tapp</h1>
This is a new Tapp
</div>
<Accordion />
</div>
)
}
}- Stateless components
const company = ({name})=> (
<Manager>
{getWage(name)}
</Manager>
);- optional static methods
- constructor
- getChildContext
- componentDidMount
- shouldComponentUpdate (only in Component)
- componentDidUpdate
- eventHandlers like
onClickUACGroup() - getter for render like
getUACGroup() - optional render methods like
renderUACGroup() - render
- Always use camelCase for your prop names.
<User
userName = "Max Mustermann"
userId={123456789}
/>- Leave out the values when the prop is expelicity
true.
<User hidden />- Always include an alt prop on
<img>tags. - Don't use words like "image", "photo" or "picture" in
<img>alt props. - Don't use
accessKey. - Use unique IDs as
key.
- Use arrow functions if possible and useful.
- Bind event handlers for the render methods in the constructor.
export default class extends React.Component {
constructor(props) {
super(props);
this.onClickStar = this.onClickStar.bind(this);
}
onClickStar() {
// do something
}
render() {
return (<Star onClick={this.onClickStar} />)
}
}- Don't use underscore prefix. JacaScript has no private method, so it makes less sense.
- Use a single space for self-closing tags.
<Order />- Don't use spaces in your curly braces.
<Order element={currentOrder} />- Use this alignmentstyles for your components.
<User
userName = "Max Mustermann"
userId={123456789}
/>// only one property
<Order id="1234567891011" />//children
<User
userName = "Max Mustermann"
userId={123456789}
>
<Orders />
</User>- Use single quotes (') for JavaScript and double quotes (") for JSX.
<User
userName = "Max Mustermann"
userId={123456789}
style = {{ margin: '10px' }}
/>- Wrap JSX in parentheses when they span more then one line.
render(){
return (
<div>
<User
userName = "Max Mustermann"
userId={123456789}
style = {{ margin: '10px' }}
/>
</div>
)
}render(){
return ( <Order>{ItemName}</Order> )
}- Use self-close tags when the tag has no child.
- If the component has multi-line properties, set the closing tag in a new line.
<div className="tapp" /><div
className = "tapp"
style = {{ overflow : 'hidden' }}
/>- Use ref callbacks.
<User ref={(ref) => {this.userRef = ref}} />- By using chayns® you have to be sure chayns® is ready. Therefor put the
ReactDOM.render()in yourchayns.ready.
chayns.ready.then(function resolved() {
ReactDOM.render(
<div className="tapp">
<Intro />
</div>,
document.querySelector('#app')
);
}).catch(function rejected() {
console.log('no chayns environment found');
}).then(function always() {
console.log('Will always be executed');
});- Our StyleGuide based on Airbnb React/JSX Style Guide