0.0.4 • Published 7 years ago
redux-adapter v0.0.4
REDUX-ADAPTER
- spliting and writing actions and reducers into pieces base on components.
- automatically add and remove reducers of redux store with component lifecycle.
- no concern of
unique action type.
usage
Actions and reducers will organize in this way.
// state transitions
export default {
// initial state
initialState: {
tasks: []
},
// transition events
events: {
addTodo: {
action(title) {
const task = { title, done: false };
return task;
},
reducer(state, task) {
state.tasks.push(task);
return state;
}
}
}
};install and coding
install:
npm i --save redux-adapterfirst step, init adapter:
import { createStore, combineReducers } from 'redux';
import ReduxAdapter from 'redux-adapter';
const initialState = {};
const initialReducers = {};
const store = createStore(
combineReducers({ ...initialReducers }),
initialState
);
ReduxAdapter.init(store, initialReducers);
export { store };transitions(actions and reducers), filename ./todoList.state.js:
export default {
name: 'todo-list',
initialState: {
tasks: []
},
events: {
addTodo: {
action(title) {
const task = { title, completed: false };
return task;
},
reducer(state, task) {
state.tasks.push(task);
return state;
}
}
}
};wrapped component ./TodoListComponent.jsx:
import React from 'react';
import { connect } from 'react-redux';
import { getState, react as reduxApdapter } from 'redux-adapter';
import stateTransitions from './todoList.redux';
import { store } from './store';
const mapStateToProp = (state, ownProps) => {
var currentState = getState(state, ownProps);
return { tasks: currentState.tasks };
};
@connect(mapStateToProp)
@reduxApdapter(reduxTransitions)
export class TodoListComponent extends Component {
...
addTodo(todo) {
const { dispatch, actions } = this.props;
dispatch(actions.addTodo(todo));
}
...
}Tips
- when coding the the transition, you will never care about the
action.type, just name your transition and put theactionandreducerin theeventsnamespace. reducer will recive what theactionreturn in secode argument, first arguement isstate, just keep same to the redux official.
it works when
componentDidMountand destroy whencomponentWillUnmount.you can access the state from global state in
mapStateToPropfunciton using 'getState' exported fromredux-adapter.
- in the wrapped component,
reduxAdapterIdandactionswill add tothis.propsautomatically.actionsis all the actionCreators you custom inxxx.redux.js, you can dispatch it easy, e.g.
this.props.dispatch(this.props.actions.addTodo('eating'));- recomment giving a unique
redux-adapter-idfor each component, if you want to save and restore state. Because multiple component at the same time.ReduxApdaterwill find the id in props order in:redux-adapter-id->id, if you don't assign this,redux-adapterwill useing a global auto increasement number for this.
<SomeComponentUsingReduxAdapter redux-adapter-id="component-1" />
<SomeComponentUsingReduxAdapter redux-adapter-id="component-2" />The MIT License (MIT).
Written by qinyuanbin.
0.0.5-0
7 years ago
0.0.4
7 years ago
0.0.4-1
7 years ago
0.0.3
7 years ago
0.0.2-12
7 years ago
0.0.2-11
7 years ago
0.0.2-10
7 years ago
0.0.2-9
7 years ago
0.0.2-8
7 years ago
0.0.2-7
7 years ago
0.0.2-6
7 years ago
0.0.2-5
7 years ago
0.0.2-4
7 years ago
0.0.2-3
7 years ago
0.0.2-2
7 years ago
0.0.2-1
7 years ago
0.0.1-8
7 years ago
0.0.1-7
7 years ago
0.0.1-6
7 years ago
0.0.1-5
7 years ago
0.0.1-4
7 years ago
0.0.1-3
7 years ago
0.0.1-2
7 years ago
0.0.1-1
7 years ago
0.0.1-0
7 years ago
1.1.1
7 years ago