Latest Posts

Introduction React useContext is very handy to avoid prop drilling. It can be used to define global state or shared state that multiple components in the tree can access. However, useContext is not specifically designed for global state and there’s a caveat. Any change to context value propagates all useContext to re-render components. This post shows some example code about the problem and the solution with state usage tracking. Problem Let’s assume a person object as a state. ... Read More
Introduction React hooks have changed the way to compose components. This post will show an example which is very hooks-oriented. We use two libraries: react-tracked and immer. While immer makes it easy to update state in an immutable way, react-tracked makes it easy to read state with tracking for optimization. Please check out the repo for more details. The example we show is the one from Redux: Todo List ... Read More
Introduction There are many libraries for global state with React hooks. React Redux also provides hooks API, which is very clean. In general, I would avoid using global state. It would reduce the isolation of components. Multiple contexts should work fine for certain use cases. But, what if we really need a global state. Problem When a state is a non-trivial object, it’s not likely to use all properties of the object for one component to render. ... Read More
Introduction Since React hooks landed, there has been many libraries proposed for global state. Some of them are simple wrappers around context. Whereas, some of them are full featured state management systems. Technically, there are several implementations how to store state and notify changes. We don’t go in detail in this post, but just note two axes. whether context based or external store whether subscriptions based or context propagation In this post, we focus on API design of hooks at the consumer end. ... Read More
Introduction useReducer is a powerful hook. It’s known that useState is implemented with useReducer. In the React hooks docs, it’s noted like this: useReducer is usually preferable to useState when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one. useReducer also lets you optimize performance for components that trigger deep updates because you can pass dispatch down instead of callbacks. ... Read More
Introduction React-Redux provides hooks API with nice abstraction. Especially, useSelector is probaly less misused than mapStateToProps. react-tracked is a library for global state without Redux. This library provides almost compatible hooks API to React-Redux. It’s developed with performance in mind, and it should be as performant as React-Redux, even though it utilizes only React context. See the GitHub repo for more information. This post shows benchmark results to convince that it actually is performant in one scenario. ... Read More
Introduction There are many libraries to provide global state in React. React itself doesn’t provide such a feature, probably because separation of concerns is important and having global state naively is not idiomatic. However, in certain cases, having global state is good as long as it’s properly implemented. It’s likely that performance drops down compared to using non-global state (incl. multiple contexts). This post introduces a library for global state with performance. ... Read More
Introduction If you had already used Redux and loved it, you might not understand why people try using React context and hooks to replace Redux (a.k.a no Redux hype). For those who would think Redux DevTools Extension and middleware are nice to have, Redux and context + hooks are actually two options. Context + hooks is just fine to share state among components, however if apps get bigger, they are likely to require Redux or other similar solutions; otherwise they end up having many contexts that can’t be handled very easily. ... Read More
Introduction Global state or shared state is one of the biggest issues when you start developing a React app. Should we use Redux? Do hooks provide a Redux-like solution? I would like to show four patterns toward using Redux. This is my personal opinion and mainly for new apps. Pattern 1: Prop passing Some might think it wouldn’t scale, but the most basic pattern should still be prop passing. If the app is small enough, define local state in a parent component and simply pass it down to child components. ... Read More
Introduction Recently, React Redux released hooks API. It’s v7.1.0-alpha.4 as of writing. On the other hand, I’ve been developing a new React Redux binding library with hooks and Proxy. It’s time to benchmark both of them to have better understanding in performance. The reactive-react-redux library utilizes Proxy to auto-detect state usage, hence it technically has overhead which would affect performance. We would like to learn how much it will be affected in a realistic example. ... Read More