Skip to content

Commit 7518557

Browse files
committed
Fixes initial state loading when initial state doesn't match model
1 parent eff3fa5 commit 7518557

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/__tests__/store.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ test('supports initial state', () => {
237237
foo: {
238238
bar: {
239239
stuff: [3, 4],
240-
invalid: 'qux',
240+
quxx: 'qux',
241241
},
242242
},
243243
};
@@ -248,6 +248,7 @@ test('supports initial state', () => {
248248
foo: {
249249
bar: {
250250
stuff: [3, 4],
251+
quxx: 'qux',
251252
},
252253
color: 'red',
253254
},
@@ -298,3 +299,18 @@ test('redux thunk configured', async () => {
298299
// assert
299300
expect(result).toBe('foo');
300301
});
302+
303+
test('initialState is respected even if not in model', () => {
304+
// act
305+
const store = createStore(
306+
{},
307+
{
308+
initialState: {
309+
foo: 'bar',
310+
},
311+
},
312+
);
313+
314+
// assert
315+
expect(store.getState().foo).toEqual('bar');
316+
});

src/create-store-internals.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default function createStoreInternals({
3535
disableInternalSelectFnMemoize,
3636
initialState,
3737
injections,
38+
isRebind,
3839
model,
3940
reducerEnhancer,
4041
references,
@@ -44,7 +45,7 @@ export default function createStoreInternals({
4445
? memoizerific(maxSelectFnMemoize)(x)
4546
: x;
4647

47-
const defaultState = {};
48+
const defaultState = isRebind ? {} : initialState || {};
4849
const actionThunks = {};
4950
const actionCreators = {};
5051
const actionCreatorDict = {};
@@ -149,12 +150,15 @@ export default function createStoreInternals({
149150
);
150151
}
151152
} else if (isStateObject(value) && Object.keys(value).length > 0) {
152-
set(path, defaultState, {});
153+
const existing = get(parentPath, defaultState);
154+
if (!existing || isRebind) {
155+
set(path, defaultState, {});
156+
}
153157
recursiveExtractDefsFromModel(value, path);
154158
} else {
155159
// State
156160
const initialParentRef = get(parentPath, initialState);
157-
if (initialParentRef && key in initialParentRef) {
161+
if (!isRebind && initialParentRef && key in initialParentRef) {
158162
set(path, defaultState, initialParentRef[key]);
159163
} else {
160164
set(path, defaultState, value);

src/create-store.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,15 @@ export default function createStore(model, options = {}) {
8989
? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
9090
: reduxCompose);
9191

92-
const bindStoreInternals = state => {
92+
const bindStoreInternals = (state, isRebind = false) => {
9393
references.internals = createStoreInternals({
9494
disableInternalSelectFnMemoize,
9595
initialState: state,
9696
injections,
9797
model: modelDefinition,
9898
reducerEnhancer,
9999
references,
100+
isRebind,
100101
});
101102
};
102103

@@ -137,7 +138,7 @@ export default function createStore(model, options = {}) {
137138
bindActionCreators(references.internals.actionCreators);
138139

139140
const rebindStore = () => {
140-
bindStoreInternals(store.getState());
141+
bindStoreInternals(store.getState(), true);
141142
store.replaceReducer(references.internals.reducer);
142143
store.dispatch.replaceState(references.internals.defaultState);
143144
bindActionCreators(references.internals.actionCreators);

0 commit comments

Comments
 (0)