Make custom React Hooks for Redux actions and dispatches
Each of us likes to make the job easier and faster because it reduces a lot of pain and saves nerves in the future. Creating your custom React hooks for Redux allows doing that and don’t create and use multiple functions that will surely affect your performance and the performance of your app as well. Create your own hook instead of the regular and boilerplate solutions and you will get a nice and reusable code that will be handy across your project.
Below you can see an example of how can it be:
You’ll notice that you don’t need to write numerous functions for every Redux action. To access any information from Redux, we can also utilize useSelector.
Object instead of switch in the reducer
In case you have a lot of switch cases and it becomes hard to handle the most suitable solution here is the usage of object literal. An object will be much more readable than switch statements and, as a result, it will be simpler to maintain.
React.lazy turns out to be a great and mighty tool that provides the opportunity to load the components only when they are needed. In that way it allows you to render a dynamic import as if it is a regular component. If you want to use that great tool, the starting point here is the React routes. When using the old way, you must load both components prior to rendering them, which is inefficient because it takes extra time for all components to load. Although we are not displaying the component.
In that case, to provide the loading of all components we will use React.lazy. So, we can see that you can load the first components when you are on the first page (Home) and the second when you’re on the second page (About).
In such a simple way we can avoid the unnecessary components’ load.
Render arrays dynamically using map
Sometimes we deal with repeated HTML blocks and arrays and in order to avoid their repetitive rendering, you can use the map method to render them dynamically.
Destructuring for React props
Rather than passing props objects, pass the prop name using the destructuring assignment. In that way, that won’t be necessary to turn to the props object each time you need it.
Stick to the DRY principle
A general rule for any code would be to keep it as short and simple as possible. To do that, all that is necessary is to stick to the DRY (Don’t Repeat Yourself) principle. This may be accomplished by looking for patterns and repetitions in the code. If you discover any, it’s conceivable you’re duplicating code and there’s room to cut it down. Most likely, some reworking will make it more succinct.
This largely relies on React’s reusability idea. If you wish to add numerous buttons with icons, rather than creating HTML for every button, you can just utilize the IconButton component as below. You could even go one step further and map everything to an array.
Don't forget about naming conventions
As far as React uses JSX we have to name the components with uppercase letters at the beginning. The main reason for it is the possibility to differentiate our components from the default HTML tags and keep the reusability of our components as well.
In that way, we have to use the Pascal case for components and the Camel case for instances to avoid any kind of confusion.
Don't use indexes as Key Props
React utilized keys to identify items in an array in a unique way. This allows React to track the changes that happen to a particular item. In that way React perfectly knows if an item was changed, removed, added to the array, and so on.
As a rule, most developers use indexes as keys while rendering an array. Sure thing it will work but if an array is going to change that approach can cause some trouble.
For example, take a look at the following array:
Right now item 1 is at index 0, but if we add another item at the beginning the index of item 1 will be one. That will affect the behavior of the array. So the best option here is to use a unique key so React could track the changes properly.
Add a CSS file
Whenever a CSS file is applied to an app, it adds beauty to your work and extends the program’s life due to the style rules we provide. React allows you to include CSS both inline and outside. CSS may be used to customize every section or element of your web page or app. After you’ve made modifications to the CSS file, use the command “import ‘./App.css” to import files anywhere they’re needed.
Reusability is a must
One of the most important rules during React development is “one function = one component”. You have to do your components reusable to reduce the amount of time and unnecessary coding. Having such reusable components allows you to use them across different projects and achieve consistency. Also, try to modularize your components to make them simpler and more flexible because in that way the range of their reusability and usefulness will be much higher.
Below you can see the Button component that handles icons. So, every time you need a button you have a ready component to use. And splitting it into smaller modules allows you to cover numerous cases with just one code.
We as a ReactJS development company highly recommend you stick to these practices. Our React developers use them on a daily basis, and this is one more thing that makes our project successful. If RIVO AGENCY is successful with these approaches, then you can be too. Go ahead and try them out!
Don't want to miss anything?
Get weekly updates on the newest design stories, case studies and tips right in your mailbox.