6.46.8 • Published 4 days ago

@gasket/redux v6.46.8

Weekly downloads
38
License
MIT
Repository
github
Last release
4 days ago

@gasket/redux

Redux configuration for Gasket apps.

Installation

npm i @gasket/redux

Functions

  • configureMakeStore(options, [postCreate]) - Returns a makeStore function

Parameters

  • options - (object) Options object
    • initialState - (object) Optionally set any preloaded state
    • reducers - (object) Map of identifiers and reducer functions
    • middleware - (function[]) Additional redux middleware to apply
    • enhancers - (function[]) Any other redux store enhancers
    • logging - (boolean) set to true if you want to enable redux logger. (default: false)
  • postCreate - (function) Executed after the store is create the resulting store as the argument

Return Value

  • makeStore - (function) Creates the redux store for each server-side request and once on the client, hydrating with the state from the server.

Usage

Gasket apps ship with a default a redux configuration which includes the redux-thunk middleware. This should be sufficient for most app needs, however the configureMakeStore can be used to do any additional configuration. The most common use case is to add reducers as the app level.

By default, custom store configurations can be placed in a store.js at the root of your app. If you wish for it to reside elsewhere, direct the redux.makeStore property to it in your app's gasket.config.js. Custom make-store files should use CommonJS imports since they will be executed in NodeJS for SSR.

Example: adding reducers

// ./store.js

const { configureMakeStore } = require('@gasket/redux');
const reducers = require('./reducers'); // apps reducers

module.exports = configureMakeStore({ reducers });

Example: adding middleware in a custom path (redux-saga)

// ./lib/make-store.js

const sagaMiddleWare = require('redux-saga').default();
const { configureMakeStore } = require('@gasket/redux');
const reducers = require('../reducers');
const rootSaga = require('../sagas');

const middleware = [sagaMiddleWare];

module.exports = configureMakeStore({ reducers, middleware }, store => {
  // The method below is only needed if you are utilizing
  // next-redux-saga wrapper for handling sagas in `getInitialProps`
  // store.runSagaTask = (saga) => {
  //   store.sagaTask = sagaMiddleWare.run(saga);
  // };
  // store.runSagaTask(rootSaga);

  // This is needed to initialize sagas
  store.sagaTask = sagaMiddleWare.run(saga);
});

Next in the gasket.config.js, set the redux.makeStore field to the file. This will start up the app using the custom configuration.

// gasket.config.js

module.exports = {
  redux: {
    makeStore: './lib/make-store.js'
  }
};

Example: passing custom thunk middleware

The default thunk middleware can be overridden with a customized version by passing thunkMiddleware. A common use case for this is to use the withExtraArgument feature of redux-thunk.

// ./store.js

const { configureMakeStore } = require('@gasket/redux');
const reducers = require('./reducers');
const thunk = require('redux-thunk');

const myExtraArg = {};
const thunkMiddleware = thunk.withExtraArgument(myExtraArg);

module.exports = configureMakeStore({ reducers, thunkMiddleware })

License

MIT

7.0.0-next.21

4 days ago

7.0.0-next.9

9 days ago

7.0.0-next.10

9 days ago

7.0.0-next.7

10 days ago

7.0.0-next.11

9 days ago

7.0.0-next.8

9 days ago

7.0.0-next.12

9 days ago

7.0.0-next.13

9 days ago

7.0.0-next.6

10 days ago

7.0.0-next.20

9 days ago

7.0.0-next.5

10 days ago

6.46.8

11 days ago

7.0.0-next.4

12 days ago

7.0.0-next.3

16 days ago

7.0.0-next.2

23 days ago

7.0.0-next.1

24 days ago

7.0.0-next.0

24 days ago

7.0.0-cli.7

2 months ago

7.0.0-cli.6

2 months ago

6.46.1-cli.0

2 months ago

7.0.0-cli.5

2 months ago

7.0.0-cli.4

2 months ago

7.0.0-cli.1

2 months ago

7.0.0-cli.0

2 months ago

7.0.0-cli.3

2 months ago

7.0.0-cli.2

2 months ago

7.0.0-canary.1

2 months ago

6.46.0

3 months ago

6.45.2

3 months ago

6.45.0

4 months ago

6.39.0

12 months ago

6.38.1

1 year ago

6.36.1

1 year ago

6.38.0

1 year ago

6.34.6

2 years ago

6.34.3

2 years ago

6.34.4

2 years ago

6.34.2

2 years ago

6.30.0

2 years ago

6.21.0

2 years ago

6.24.0

2 years ago

6.20.2

2 years ago

6.10.1

2 years ago

6.10.0

2 years ago

6.14.0

2 years ago

6.17.0

2 years ago

6.17.1

2 years ago

6.1.0

3 years ago

6.0.12

3 years ago

6.0.8

3 years ago

6.0.7

3 years ago

6.0.0

3 years ago

6.0.0-canary.13

3 years ago

6.0.0-canary.2

4 years ago

6.0.0-canary.1

4 years ago

6.0.0-canary.0

4 years ago

5.6.0

4 years ago

5.0.2

4 years ago

5.0.0

4 years ago

5.0.0-canary.4

4 years ago

5.0.0-canary.3

4 years ago

5.0.0-canary.2

4 years ago

5.0.0-canary.1

4 years ago

5.0.0-canary.0

4 years ago