This article will discuss code reviews for React code. Code reviews are a process that usually occurs once a developer is ready to merge their work into a branch that other developers are also working in. It acts as a QA (quality assurance) gate to hopefully reduce bugs in a system.
First, we need to understand a code review and its purpose.
What is a code review?
A code review is a process of reviewing source code to identify and fix problems and improve the quality of the code. There will be a specific checkpoint where code reviews occur in many software teams. This is often part of a PR (pull request) where users can approve and suggest fixes to someone’s code. In smaller teams, there may not be such stringent checkpoints, and it may be done on the fly before new code is deployed to production.
- Finding bugs: Having people review the code makes it more likely that any problems or bugs will be caught before the code is deployed.
- Knowledge sharing: Code reviews are an excellent opportunity for team members to learn from each other and share best practices.
- Collaboration: Code reviews facilitate collaboration between team members and help ensure that the codebase is consistent and meets the team’s standards.
- Higher code coverage: Code reviews can help ensure that all parts of the codebase are covered by tests, which can increase the overall reliability of the code.
Reviewing React Code
Code reviews for React applications can follow similar processes to code reviews for other applications. Some specific things that reviewers might look for when reviewing React code include the following:
- Proper use of React concepts such as state, props, and lifecycle methods
- Code splitting and lazy loading to optimise performance
- Use of functional components and hooks where appropriate
- Adherence to coding standards and best practices, such as using prop types to ensure that components receive the correct props
- Adequate testing of components using tools like Jest and Enzyme
It’s also essential to ensure that the code is well-organized and easy to understand, with a clear function, variable names, and appropriate comments. Reviewers may also look for opportunities to refactor the code to make it more maintainable or scalable.
Performing code reviews on React code is very similar to any other language or framework code review. The typical components of a code review look for any obvious mistakes but then drill down to coding standards, reuse of code, appropriate placement within the project etc. Most importantly, (at least in my opinion) code reviews allow multiple developers to look over each other’s code. A lot of learning goes on in this process (and we never stop learning), even for reviews where there is nothing to comment on and are approved straight away.
Understanding the State Hook
Learn about the React useState State hook. Take a brief look at how useState was implemented in the playing card