amit kumar
/amit08255
Welcome to my Social Blog
amit kumar
4y ago
Writing great code doesn't have to be complex. These simple tips will help you build React apps that are very easy to test, update and are super flexible.
Unfortunately we often build apps without thinking about how it will evolve to future changes.
How these techniques will be a life-saver
Here are some common scenarios in which these small techniques will prove a life-saver:
Easy to test.
Adapt to future updates.
Separation of business logic.
Predictable data flow.
Writing future-proof components
Always remember these thumb rules when writing components:
UI components should always be unaware of the network requests, business logic, or app state.
Components should be written like a pure function. It simply means that it should always render same data when provided same props.
For a detailed guide to writing great components. Check out this guide.
Making evolution easy with HOCs
React Higher Order Component (HOC) is a component that takes a component and returns a component.
Always remember these thumb rules when writing HOC:
Always prefer to create those HOCs which don't create any props. If your HOC need to create props, always make sure it is not creating more than one.
Use HOC when that behavior is used either by multiple components or all components in the app.
Make sure the component can work without HOC. Components should not create a dependency with HOC.
A component can be directly wrapped in HOC without having to add additional logic to it.
Architect your app for clean code
Never mix view render concerns, data processing and data fetching in a single place. Mixing everything will lead to a spaghetti architecture and will become painful in future.
Always make your component as dumb as possible. A component should always render same thing given the same state.
Flux architecture allows you to create a predictable data flow for your app. In this architecture event listeners listen for things like: user input handlers, network request handlers. When event listeners get them, it dispatches actions to the store. State is only be updated when an action is dispatched.
Use flux architecture based libraries like Reflux, Redux, etc. in below scenarios:
Component uses network request or device API.
Has common application states.
Component needs business logic or data manipulation logic that is shared with other parts of app.
Using flux architecture will not only allow you to build highly flexible UI components, it will also allow you to separate things like business logic from your component. It will allow you to easy migration to different data fetching techniques. The predictable unidirectional data flow will make your app easy to debug and test.
amit kumar
4y ago
These small tips can save you large amount of time when dealing with states in a complex React application.
The stale state issue is common in React. The tips we are going to discuss will help you solve this issue in different scenarios.
Some common scenarios where this issue occurs
Here are a list scenarios when you get stale states in your code:
Inside a closure.
Inside an event handler callback.
Inside asynchronous function callback.
Here is a small project which uses asynchronous function callback and notice how the state update is not working. It is all because of stale state inside the callback function.
https://codesandbox.io/s/objective-dream-2esuky
Technique 1: Using function updates
This is very simple technique which allows you to deal with stale states. Use this technique in scenarios like: asynchronous callback, or inside a closure.
Here is how it works: Pass a function to the useState dispatcher. Passing a function to the dispatcher will allow you to access previous state value. The previous state value will be passed to the function when executed. To set the new state, you just need to return the new value from the function.
Here is the update version of same app above. It just have a small change i.e. a function is passed to the useState dispatcher. Notice how it's working perfectly within asynchronous callback.
https://codesandbox.io/s/heuristic-shaw-vfqr77
You can see we have used useRef here. The benefit of using useRef is that it allows you to keep track of a value without re-rendering. Since the object returned by useRef will be attached to component instance for it's entire life, it is a great way to make sure only one setInterval callback is created.
Technique 2: Using latest ref
This technique is very useful in scenarios such as: event handler callback. Here is how it works: You create a useRef object instance by passing your callback. Since the object created by useRef is created only once for the life of a component, it allows you to keep track of any value without re-rendering.
https://codesandbox.io/s/frosty-shape-lvfr58
You can see in the above app that the useRef is used to store a callback. The useLayoutEffect is used to register and remove previous callback. useLayoutEffect works same as useEffect except that here the callback passed is called before the change happened is painted on the browser screen.
Technique 3: Using callback memoization
This technique is very similar to previous one. Instead of storing the callback in ref.current , you pass that inside useCallback. The useCallback will return a memoized reference to the callback. Similar to useEffect you can pass a list of dependencies here as well. The advantage of this technique is that you can execute a set of things when the callback reference changes.
These simple techniques are very useful which will allow you to prevent issues in your application due to stale state data.