Nuxt.js: Use initialState instead of state when exporting in modules mode

Created on 27 Sep 2018  路  8Comments  路  Source: nuxt/nuxt.js

What problem does this feature solve?

The issue I've encountered is this:

export const state = () => ({
  someState: 'foo',
});

export const actions = {
  ACTION({ dispatch, commit }) {

    // I forgot to destructure state in the function arguments
    // so now I'm accessing the state function by accident

    if (state.something) {
      // .. never true
    }
  }
};

What does the proposed changes look like?

Allow state to be exported as initialState:

export const initialState = () => ({
  someState: 'foo',
});

export const actions = {
  ACTION({ dispatch, commit }) {

    // Error can now be picked up by ESLint or will 
    // throw an error with state being undefined
  }
};

This feature request is available on Nuxt community (#c7851)
feature-request good first issue

Most helpful comment

@felubra Sorry, somehow I missed the notifications from that issue 馃

I would not deprecate state because the default VueX initialization structure should still work. It鈥檇 be a nice addition to accept initialState as well

All 8 comments

I'd like to help with this issue 馃槉

Question: should we deprecate the old behavior, the export using the 'state' function name? Or should we use both - check for a 'state' function then check for a 'initialState' function?

Perhaps deprecate the state export but support both for now and then remove state in a future release to give plenty of time for migration

@manniL I would appreciate hearing your opinion on this, thanks!

@felubra Sorry, somehow I missed the notifications from that issue 馃

I would not deprecate state because the default VueX initialization structure should still work. It鈥檇 be a nice addition to accept initialState as well

As stated in #4096, this feature won't see the daylight.

Thanks for your efforts @felubra! :pray:

This feature-request has been cancelled by @manniL.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bimohxh picture bimohxh  路  3Comments

maicong picture maicong  路  3Comments

msudgh picture msudgh  路  3Comments

lazycrazy picture lazycrazy  路  3Comments

shyamchandranmec picture shyamchandranmec  路  3Comments