Member-only story
11 Mistakes to Avoid When Using React
Some common mistakes in React development you should avoid.
As React becomes more and more popular, more and more React developers have encountered various problems in the development process.
In this article, based on my actual work experience, I will summarize some common mistakes in React development to help you avoid some mistakes.
If you are just starting to use React, it is recommended that you take a good look at this article. If you have already used React to develop projects, it is also recommended that you check and fill in the gaps.
After reading this article, you will learn how to avoid these 11 React mistakes:
- When rendering the list, do not use the key.
- Modify the state value directly by assignment.
- Bind the state value directly to the value property of the input.
- Use state directly after executing setState.
- Infinite loop when using useState + useEffect.
- Forgetting to clean up side effects in useEffect.
- Incorrect use of boolean operators.
- The component parameter type is not defined.
- Passing Strings as Values to Components.
- There is no component name that starts with a capital letter.
- Incorrect event binding for element.
1. When rendering the list, do not use the key
🎀Problem
When we first learned React, we would render a list according to the method described in the documentation, for example:
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) => <li>{number}</li>);
After rendering, the console will prompt a warning ⚠️ a key should be provided for list items
.
🎉Solutions
You just need to follow the prompts and add the key
attribute to each item:
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number, index) => <li key={index}>{number}</li>);