0.0.4 • Published 6 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-adapter
first 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 theaction
andreducer
in theevents
namespace. reducer will recive what theaction
return in secode argument, first arguement isstate
, just keep same to the redux official.
it works when
componentDidMount
and destroy whencomponentWillUnmount
.you can access the state from global state in
mapStateToProp
funciton using 'getState' exported fromredux-adapter
.
- in the wrapped component,
reduxAdapterId
andactions
will add tothis.props
automatically.actions
is 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-id
for each component, if you want to save and restore state. Because multiple component at the same time.ReduxApdater
will find the id in props order in:redux-adapter-id
->id
, if you don't assign this,redux-adapter
will 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
6 years ago
0.0.4
6 years ago
0.0.4-1
6 years ago
0.0.3
6 years ago
0.0.2-12
6 years ago
0.0.2-11
6 years ago
0.0.2-10
6 years ago
0.0.2-9
6 years ago
0.0.2-8
6 years ago
0.0.2-7
6 years ago
0.0.2-6
6 years ago
0.0.2-5
6 years ago
0.0.2-4
6 years ago
0.0.2-3
6 years ago
0.0.2-2
6 years ago
0.0.2-1
6 years ago
0.0.1-8
6 years ago
0.0.1-7
6 years ago
0.0.1-6
6 years ago
0.0.1-5
6 years ago
0.0.1-4
6 years ago
0.0.1-3
6 years ago
0.0.1-2
6 years ago
0.0.1-1
6 years ago
0.0.1-0
6 years ago
1.1.1
6 years ago