From 04ffdd04e3f53508ea5a5d23aa4daed6866e168d Mon Sep 17 00:00:00 2001 From: Nikita Tolkachev Date: Thu, 31 Aug 2017 23:51:22 +0300 Subject: [PATCH 1/3] assert state to be an object Just a moment ago had hard time debugging this code: ```js const state = () => {} ... new Vuex.Store({ state, modules, ... }) ``` If state is `undefined` and you use modules, Vuex can't create store. This small change will make it easier to find this error. --- src/store.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/store.js b/src/store.js index fce8d60ae..c224bdf95 100644 --- a/src/store.js +++ b/src/store.js @@ -24,6 +24,10 @@ export class Store { if (typeof state === 'function') { state = state() } + + if (process.env.NODE_ENV !== 'production') { + assert(state != null, `State must be an object.`) + } // store internal state this._committing = false From 671156581f77c193d63c1ff80c2a222bc0609219 Mon Sep 17 00:00:00 2001 From: Nikita Tolkachev Date: Thu, 31 Aug 2017 23:55:48 +0300 Subject: [PATCH 2/3] fix styling --- src/store.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/store.js b/src/store.js index c224bdf95..c4959035c 100644 --- a/src/store.js +++ b/src/store.js @@ -24,7 +24,7 @@ export class Store { if (typeof state === 'function') { state = state() } - + if (process.env.NODE_ENV !== 'production') { assert(state != null, `State must be an object.`) } From 16d8d4c7669ea205bced90b73efd2566cd08d14d Mon Sep 17 00:00:00 2001 From: Nikita Tolkachev Date: Fri, 1 Sep 2017 03:59:02 +0300 Subject: [PATCH 3/3] provide default value for state as a function --- src/store.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/store.js b/src/store.js index c4959035c..a5b694f55 100644 --- a/src/store.js +++ b/src/store.js @@ -22,11 +22,7 @@ export class Store { state = {} } = options if (typeof state === 'function') { - state = state() - } - - if (process.env.NODE_ENV !== 'production') { - assert(state != null, `State must be an object.`) + state = state() || {} } // store internal state