React Rendering #3 — Rendering State

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

If you don’t have time to read but want to know what’s there in this post. Find the quick read 👇

In this React rendering series, we will cover the following
React rendering basics
React rendering optimization
React rendering state
In this React rendering post, we are going to see the following
Context API
Context Optimization
React-Redux
React-Redux optimization
React’s Context API provides a way to pass data through the component tree without using props, but should not be used for state management as it requires manual updating. Any component inside a context’s Provider can access the data in the context instance using a Consumer component or, for function components only, the **useContext** hook.
When a new reference is passed to a context Provider it will cause any connected components to update. React will look for any components consuming the context in the component tree and update them to reflect the change in the context’s value. Passing a new object to a context Provider is essentially a new reference, as the context holds a single value.
By default, any update to a parent component that renders a context Provider will cause all of the child components to re-render regardless of changes in the context, due to React’s rendering process. To avoid re-rendering child components when a parent changes, memoization can be used, which will cause React to skip the whole subtree of a skipped component.
When the context is updated, React additionally checks for components consuming the context down the subtree. This allows context-consuming components under a memoized parent that does not re-render to consume the updated context and render as necessary. After a context-consuming component re-renders, React will keep on recursively rendering its child components as usual.
Oftentimes, it’s a good idea to memorize the component immediately under a context Provider. That way updates to the parent component will not cause a re-render for the whole subtree, but only the components that consume the context.
React-Redux provides bindings for Redux, a state container for JavaScript applications, and works a little differently from React’s Context API. One of the key differences is that React-Redux only re-renders components that need to render, due to the fact that components subscribed to the Redux store read the latest store state, diff the values, and force re-render only if the relevant data has changed, while React is not involved at all in the subscription callback process.
React-Redux always executes its mapStateToProps and useSelector functions for every connected component in the tree whenever the store state is updated. These calculations are usually less expensive than React’s rendering, but if there are costly calculations performed or new references returned when they shouldn’t, it might become problematic.
React-Redux provides two ways of connecting to its store, performing the necessary work, and returning the combined props
connect (any component): **Higher-order component** (HOC) that wraps any given component
useSelector (function components): Hook called inside function components
connect acts a lot like memoizing a React component (i.e. using React.PureComponent or React.memo()), updating the wrapped component only when the combined props have changed. This means that passing new references from the parent or the passed functions will still cause a re-render.
Components wrapped with connecting usually read smaller pieces of data from the store state and are less likely to re-render due to that, and usually affect fewer components down their tree.
useSelector has no way of stopping a component from rendering when its parent component renders. When exclusively using useSelector, larger parts of the component tree will re-render due to Redux store updates than they would with connect, since there aren’t other components using connect to prevent them from doing so. You can use React.memo() as necessary, to optimize this behaviour by preventing unnecessary re-rendering.

And that’s the end of this series will see a new interesting craft. Till then Happy Learning :)