Discover What's Inside
ES6 features
Arrow functions: Arrow functions are a concise way to write functions in JavaScript. They use the “=>” syntax and automatically bind “this” to the enclosing scope.
Example:
const MyComponent = () => {
return (
<div>
<h1>Hello, world!</h1>
</div>
);
}
Template literals: Template literals are a new way to write strings in JavaScript that allows you to embed variables and expressions in them using the ${} syntax.
Example:
const name = "John";
const MyComponent = () => {
return (
<div>
<h1>Hello, ${name}!</h1>
</div>
);
}
Destructuring: Destructuring is a way to extract values from objects and arrays into separate variables.
Example:
const person = {
name: "John",
age: 30
};
const { name, age } = person;
console.log(name); // "John"
console.log(age); // 30
Spread operator: This can be useful for merging arrays or passing props to components.
Example:
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4, 5, 6]
Use PropTypes
PropTypes is a package that helps you to type-check your React components’ props. It can help you catch errors early and prevent bugs. You can define the expected types of your props using PropTypes.
import PropTypes from 'prop-types';
function MyComponent(props) {
return (
<div>
<h1>{props.title}</h1>
<p>{props.text}</p>
</div>
);
}
MyComponent.propTypes = {
title: PropTypes.string.isRequired,
text: PropTypes.string.isRequired,
};
Functional components
Functional components are simpler and more concise than class components. They are easier to read, write, and test. You can use functional components whenever possible.
function MyComponent(props) {
return (
<div>
<h1>{props.title}</h1>
<p>{props.text}</p>
</div>
);
}
React.memo
React.memo is a higher-order component that can help you optimize your component performance. It memoizes the result of a component rendering, so it only re-renders if the props have changed.
import React, { memo } from 'react';
const MyComponent = memo((props) => {
return (
<div>
<h1>{props.title}</h1>
<p>{props.text}</p>
</div>
);
});
Meaningful variable and function names
Meaningful variable and function names can make your code easier to understand and maintain. Use descriptive names that explain what the variable or function does.
// Bad variable name
const a = 10;
// Good variable name
const numberOfUsers = 10;
// Bad function name
function f() {}
// Good function name
function getUserData(userId) {}
CSS modules
CSS modules can help you write modular and maintainable CSS code. They allow you to scope CSS styles to a specific component and prevent CSS class name collisions.
import styles from './MyComponent.module.css';
function MyComponent(props) {
return (
<div className={styles.container}>
<h1 className={styles.title}>{props.title}</h1>
<p className={styles.text}>{props.text}</p>
</div>
);
}
Conditional rendering
Conditional rendering allows you to show or hide components based on a condition. It can help you write more flexible and dynamic React components
function MyComponent(props) {
return (
<div>
{props.isVisible && (
<div>
<h1>{props.title}</h1>
<p>{props.text}</p>
</div>
)}
</div>
);
}
State management libraries
State management libraries like Redux and MobX can help you manage the state of your React application in a more organized and scalable way. They provide a centralized store to manage your application state.
import { createStore } from 'redux';
const initialState = { count: 0 };
function reducer(state = initialState, action) {
switch (action.type) {
case 'INCREMENT':
return { ...state, count: state.count + 1 };
case 'DECREMENT':
return { ...state, count: state.count - 1 };
default:
return state;
}
}
const store = createStore(reducer);
function MyComponent(props) {
const count = useSelector((state) => state.count);
const dispatch = useDispatch();
return (
<div>
<h1>{count}</h1>
<button onClick={() => dispatch({ type: 'INCREMENT' })}>+</button>
<button onClick={() => dispatch({ type: 'DECREMENT' })}>-</button>
</div>
);
}
Write unit tests
Unit tests can help you catch bugs early and ensure that your code is working as expected. You can use testing frameworks like Jest and Enzyme to write unit tests for your React components.
import { render, screen } from '@testing-library/react';
import MyComponent from './MyComponent';
describe('MyComponent', () => {
test('renders title and text', () => {
render(<MyComponent title="Hello" text="World" />);
expect(screen.getByText('Hello')).toBeInTheDocument();
expect(screen.getByText('World')).toBeInTheDocument();
});
});