resourceReducer(resourceName, [options])
Creates a Redux reducer that changes your store's state when actions with one of the CRUD Action types are dispatched.
Arguments
resourceName
(String): The name of your resource. Typically, you'll want to use a plural name. For instance, "books," rather than "book." You should also use this as the name of your store slice when using combineReducers, for consistency.[
options
] (Object): Options that can be used to configure the reducer. The options are:initialState
: Initial state to shallowly merge into the default initial state for the slice of the store.plugins
: An array of reducer functions that will be called after the default reducer function. Use this to augment the behavior of the built-in reducer, or to add support for custom action types for this store slice. Plugins are functions that are called with the arguments(state, action, options)
, whereoptions
are the same options that you passed toresourceReducer
. For more, refer to the Plugins documentation.initialResourceMeta
: Additional metadata to include on any new resource's metadata after a read or create operation.
Returns
(Reducer
): A reducing
function for this resource.
Example
import { createStore, combineReducers } from 'redux';
import { resourceReducer } from 'redux-resource';
let store = createStore(
combineReducers({
books: resourceReducer('books'),
users: resourceReducer('users')
})
);
Tips
- Any options you pass to the
resourceReducer
will also be passed to the plugins. You can use this fact to add your own customoptions
to configure the behavior of your plugins. To learn more about plugins, refer to the Plugins guide.