9 Tweets 1 reads Mar 15, 2023
Common reactjs mistakes to avoid:
πŸ“Œ thread..
1. Not creating enough components
A common mistake that React developers make is that they don’t create enough components. It is much better to create smaller components with each of them performing a single function. Helps a lot in saving time during debugging.
2. It's not a good idea to modify the state directly
State in React should be immutable - you shouldn’t modify state directly, because doing so can cause errors and performance issues that are difficult to debug.
To update the state, use either the setState() method in class-based components or the useState() hook with functional components. We can rewrite the previous example to work as expected with the useState() method.
3. Not writing unit tests
A unit test enables you to test parts of your application freely to ensure a specific functionality works as expected. Writing a unit test saves you the time you’d spend finding that bug by pointing it out immediately. They help in debugging rapidly.
4. Forgetting that setState is asynchronous
Another common mistake is trying to access state value right after setting it.
Accessing a state value after setting it might not reflect the latest updates. This issue can be fixed by using an optional second argument to setState, which is a callback function, called after the state has been updated with its latest values.
5. Using Redux or Flux to manage all your application states
It is not recommended to use redux in every state in your application.
Redux could be hard to understand at the beginning but it will make your components share states easily. So it will make your development easy.
That's a wrap!
If you enjoyed this thread:
1. Follow me @codedamncom for more of these
2. RT the tweet below to share this thread with your audience

Loading suggestions...