Initial state in React with Redux not working
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When Redux initial state appears to be missing, the reducer is often not the real culprit. Most cases come from one of a few predictable problems: the reducer never returns a default value, the store is hydrated with different data, or the React component is selecting the wrong path from the store.
Make Sure The Reducer Can Initialize Itself
Redux initializes a slice by calling the reducer with undefined. That means the reducer must return a valid initial value in that case.
With Redux Toolkit, that usually looks like this:
If you write reducers by hand, the same rule applies:
If the reducer forgets the default parameter or accidentally returns undefined, the store cannot build the initial slice correctly.
Check The Actual Store Shape
Another common issue is that the initial state exists, but the component reads the wrong key. Once reducers are combined, the shape seen by React is often deeper than expected.
If the store is configured like this:
Then the component must select from state.counter, not from state directly:
If you accidentally write state.value, the selector returns undefined and it looks as if the initial state never loaded.
Understand preloadedState
Reducer defaults are only used when that slice is undefined. If the store is created with preloadedState, that data wins.
This is expected and often desirable for persisted sessions, tests, or server rendering. It also explains why changing initialState in the reducer sometimes seems to have no effect. The reducer is not being asked to initialize that slice from scratch anymore.
If your project uses local storage or a persistence library, inspect that hydration step before changing reducer code.
Verify The React Wiring
The app must be wrapped with a Provider, and that provider must receive the same store instance you expect to debug.
If there are multiple store instances in the project, it is possible to render with one store while dispatching against another. That kind of wiring bug makes the UI look empty even when the reducer and initial state are both fine.
Redux DevTools is useful here. If the initial state is visible in DevTools but not in the component, the selector path or provider wiring is usually the problem.
Debug In The Order Redux Actually Runs
A good debugging sequence is:
- inspect the reducer and confirm it returns a default state
- inspect store creation and check for
preloadedState - inspect the store shape after combining reducers
- inspect the
useSelectorpath in the component - inspect the
Providerand store instance wiring
That order follows the actual data flow and prevents guessing.
Common Pitfalls
- Forgetting to return an initial value when the reducer receives
undefined. - Selecting
state.valuewhen the real path isstate.counter.value. - Expecting reducer defaults to override hydrated or preloaded state.
- Creating multiple stores and giving React the wrong one.
- Mutating state directly in a hand-written reducer instead of returning a new object.
Summary
- Redux initial state comes from the reducer only when the slice is
undefined. - A correct reducer can still look broken if the component reads the wrong store path.
- '
preloadedStateoverrides reducer defaults for the data it provides.' - If the store looks correct in DevTools but not in the UI, inspect selectors and
Providerwiring next.
Related reading
- input typenumber/ is not showing a number keypad on iOS
- Insert HTML into view from AngularJS controller
- Install node in Dockerfile?
- Installing NPM on AWS EC2
- invalid ELF header when using the nodejs ref module on AWS Lambda
- Invariant Violation Application AwesomeProject has not been registered When building for iOS device with static jsbundle
- Invoke amazon lambda function from node app
- iOS JavaScript bridge
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.