### Install redux-act-reducer Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Instructions for installing the `redux-act-reducer` package using npm. ```Shell npm install redux-act-reducer --save ``` -------------------------------- ### createReducer with autoAssign and Explicit SUCCESS Handler Structure Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Shows the explicit structure for defining the SUCCESS handler within createReducer when autoAssign: true is enabled. This example clarifies the internal representation that autoAssign simplifies, where sub-handlers for SUCCESS, REQUEST, and FAILURE can be nested. ```javascript import { createReducer } from 'redux-act-reducer'; import { SHOW_HELLO_ASYNC } from '../your/actions/path'; const defaultState = {}; const hello = createReducer({ [SHOW_HELLO_ASYNC](state, action) { return { 'SUCCESS'() { return { res: action.res }; } }; }, ...... }, defaultState, { autoAssign: true }); export default hello; ``` -------------------------------- ### createReducer API Reference Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md API documentation for the `createReducer` function, detailing its parameters and options for generating Redux reducers. ```APIDOC createReducer(handlers: Object, defaultState: any, options?: Object): Function handlers: An object where keys are action types and values are reducer functions (state, action) => newState. defaultState: The initial state for the reducer. options: autoAssign: boolean (default: false). If true, automatically merges returned objects into the state using Object.assign. Returns: A Redux reducer function. ``` -------------------------------- ### createAction API Reference Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md API documentation for the `createAction` function, detailing its parameters and how it generates action creators. ```APIDOC createAction(type: string, ...args: string[]): Function type: The action type string. ...args: Optional strings representing argument names that will map to payload properties. Returns: An action creator function. ``` -------------------------------- ### Simplify Async Reducer with createReducer and autoAssign Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Explains how createReducer with the autoAssign: true option simplifies reducer logic for asynchronous actions. It demonstrates that by defining only the SUCCESS case, createReducer automatically generates the REQUEST and FAILURE handling logic, reducing boilerplate. ```javascript import { createReducer } from 'redux-act-reducer'; import { SHOW_HELLO_ASYNC } from '../your/actions/path'; const defaultState = {}; const hello = createReducer({ [SHOW_HELLO_ASYNC](state, action) { return { res: action.res }; }, // Default is SUCCESS code. // Will automatically generate REQUEST and FAILURE code, so that the code is simple. ...... }, defaultState, { autoAssign: true }); export default hello; ``` -------------------------------- ### Define Redux Actions with createAction Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Demonstrates how to use `createAction` to define action creators with predefined argument names, which map directly to action payload properties. Shows how `dispatch` calls generate the corresponding action objects, including a simplified usage without explicit argument names. ```Javascript import { createAction } from 'redux-act-reducer'; export const SHOW_HELLO = 'SHOW_HELLO'; export const showHello = createAction(SHOW_HELLO, 'info'); export const SHOW_HI = 'SHOW_HI'; export const showHi = createAction(SHOW_HI, 'arg1', 'arg2') ``` ```Javascript dispatch(showHello('Hello World!')); // dispatch action: { type: 'SHOW_HELLO', info: 'Hello World!' } dispatch(showHi('one', 'two')); // dispatch action: { type: 'SHOW_HI', arg1: 'one', arg2: 'two' } ``` ```Javascript import { createAction } from 'redux-act-reducer'; export const SHOW_HELLO = 'SHOW_HELLO'; export const showHello = createAction(SHOW_HELLO); ``` ```Javascript dispatch(showHello({info: 'Hello World!'})); // dispatch action: { type: 'SHOW_HELLO', info: 'Hello World!' } ``` -------------------------------- ### Create Redux Reducers with createReducer Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Illustrates how to use `createReducer` to define a reducer function that handles different action types. It shows how to define handlers for specific action types and manage the state immutably using `Object.assign`. ```Javascript import { createReducer } from 'redux-act-reducer'; import { SHOW_HELLO, SHOW_HI } from '../your/actions/path'; const defaultState = { info: '' }; const hello = createReducer({ [SHOW_HELLO](state, action) { return Object.assign({}, state, { info: action.info }); }, [SHOW_HI](state, action) { return Object.assign({}, state, { arg1: action.arg1, arg2: action.arg2 }); }, ...... }, defaultState); export default hello; ``` ```Javascript dispatch(showHello('Hello World!')); // state: { hello: { info: 'Hello World!' }, ... } ``` -------------------------------- ### Create Reducers with Immutable.js Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Shows how to integrate `createReducer` with Immutable.js, ensuring that state updates are handled immutably using Immutable.js methods like `set`. The `autoAssign` option must be `false` (which is the default) when using Immutable.js. ```Javascript import { fromJS } from 'immutable'; import { createReducer } from 'redux-act-reducer'; import { SHOW_HELLO } from '../your/actions/path'; const defaultState = fromJS({ info: '' }); const hello = createReducer({ [SHOW_HELLO](state, action) { return state.set('info', action.info); }, ...... }, defaultState); export default hello; ``` -------------------------------- ### createActionAsync Function API Reference Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Documents the createActionAsync function, its parameters (type, api, options), and the detailed structure of the options object for configuring automatic action dispatching and lifecycle callbacks. It highlights how api should return a promise and explains the purpose of isCreateRequest, isCreateSuccess, isCreateFailure, onRequest, onSuccess, and onFailure options. ```APIDOC createActionAsync(type, api, options) works with redux-thunk - `type` action type - `api` a module that sends requests to a server (Please return a promise) - `options` (string or object. if it is a string, it is async name) * `name` async name (default: same as `type`) * `isCreateRequest` whether to create and dispatch REQUEST action automatically (default: true) * `isCreateSuccess` whether to create and dispatch SUCCESS action automatically (default: true) * `isCreateFailure` whether to create and dispatch FAILURE action automatically (default: true) * `onRequest` function after REQUEST: onRequest(dispatch, getState) * `onSuccess` function after SUCCESS: onSuccess(dispatch, getState, res) * `onFailure` function after FAILURE: onFailure(dispatch, getState, err) ``` -------------------------------- ### Basic Usage of createActionAsync for Redux Actions Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Demonstrates how to define an asynchronous action creator using createActionAsync and how dispatching it automatically triggers REQUEST, SUCCESS, and FAILURE actions with appropriate payloads. It clarifies that arguments passed to the dispatched action creator are forwarded to the api function. ```javascript import { createActionAsync } from 'redux-act-reducer'; export const SHOW_HELLO_ASYNC = 'SHOW_HELLO_ASYNC'; export const showHelloAsync = createActionAsync(SHOW_HELLO_ASYNC, api, 'asyncName'); // api is a module that sends requests to a server. // createActionAsync will create 3 action with subType: REQUEST, SUCCESS, FAILURE // 'hello' is Asynchronous name ``` ```javascript dispatch(showHelloAsync(arg1, arg2)); // will dispatch: // dispatch({ type: 'SHOW_HELLO_ASYNC', subType: 'REQUEST', ... }); // if success: dispatch({ type: 'SHOW_HELLO_ASYNC', subType: 'SUCCESS', res, ... }); // if error: dispatch({ type: 'SHOW_HELLO_ASYNC', subType: 'FAILURE', err, ... }); // args will pass to api: api(arg1, arg2) ``` -------------------------------- ### Full Asynchronous State Update Logic with createReducer and autoAssign Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Presents the complete reducer logic for handling REQUEST, SUCCESS, and FAILURE sub-actions within createReducer. This includes updating an asyncStatus object to track isFetching status and potential errors, demonstrating the comprehensive state management that autoAssign: true can automatically generate or simplify. ```javascript import { createReducer } from 'redux-act-reducer'; import { SHOW_HELLO_ASYNC } from '../your/actions/path'; const defaultState = {}; const hello = createReducer({ [SHOW_HELLO_ASYNC](state, action) { return { 'REQUEST'() { return { asyncStatus: { hello: { isFetching: true, err: undefined } } }; }, 'SUCCESS'() { return { asyncStatus: { hello: { isFetching: false, err: undefined } }, res: action.res }; }, 'FAILURE'() { return { asyncStatus: { hello: { isFetching: false, err: action.err } } }; } }; }, ...... }, defaultState, { autoAssign: true }); export default hello; ``` -------------------------------- ### Create Reducers with autoAssign Option Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Demonstrates using `createReducer` with the `autoAssign: true` option, which automatically merges the returned object into the state, simplifying state updates by removing the need for explicit `Object.assign` calls. This section also clarifies the behavior of `autoAssign`. ```Javascript import { createReducer } from 'redux-act-reducer'; import { SHOW_HELLO } from '../your/actions/path'; const defaultState = { info: '' }; const hello = createReducer({ [SHOW_HELLO](state, action) { return { info: action.info }; }, ...... }, defaultState, { autoAssign: true }); export default hello; ``` ```Javascript dispatch(showHello('Hello World!')); // state: { hello: { info: 'Hello World!' }, ... } ``` ```Javascript return { info: 'hi' }; ``` ```Javascript return Object.assign({}, state, { info: 'hi' }); ``` -------------------------------- ### Configure createActionAsync with Custom Lifecycle Callbacks Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Illustrates how to use the options object in createActionAsync to define custom onRequest and onSuccess functions. These callbacks allow for dispatching additional actions or performing side effects at specific stages of the asynchronous operation. ```javascript export const switchFlag = createActionAsync(SWITCH_FLAG, switchFlagApi, { name: 'switchFlag', onRequest(dispatch, getState) { dispatch(shouldUpdate(true)); }, onSuccess(dispatch, getState, res) { dispatch(shouldUpdate(false)); } }); ``` -------------------------------- ### Redux State Structure Managed by createReducer for Async Actions Source: https://github.com/hahoocn/redux-act-reducer/blob/master/README.md Illustrates the resulting structure of the Redux state when createReducer processes asynchronous actions, particularly with autoAssign: true. It shows how an asyncStatus object is maintained, tracking isFetching status and potential errors, using either the async action's name or type as a key. ```javascript state: { hello: { asyncStatus: { asyncName: { isFetching: true, err: undefined } }, ... }, ... } ``` ```javascript state: { hello: { asyncStatus: { SHOW_HELLO_ASYNC: { isFetching: true, err: undefined } }, ... } ... } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.