React State Redux
Clear away states from React components.
Note: The name of this project is temporary yet.
Description
It helps us move states to the Redux store from React components, especially when your components is dynamically used in many places.
Example
Suppose we have a simple textfield component like this:
{ superprops thisstate = value: "" } { this } { return <div> <input type="text" value=thisstatevalue onChange=thishandleChange /> </div> }
We can rewrite it as a stateless component like this:
const mapStateToProps = value: componentState { } const TextField = <div> <input type="text" value=value onChange= /></div> mapStateToPropsTextField componentReducer
Installation
npm install --save react-state-redux
Tutorial
// Add the reducer to your store on the `reactStateRedux` keyconst store = // Prepare a component.const MyComponent = <div> <DoSomething /></div> // Prepare a reducer to handle actions and return a component state. { } // Connect to a Redux store and a component state.const ConnectedMyComponent = MyComponent componentReducer // Use the connected component.ReactDOM
API
Please see React Redux API if you have not done.
connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options]) => (component, componentReducer) => connectedComponent
mapStateToProps(state, componentState, [ownProps]) => stateProps
state
The same asReact Redux
's onecomponentState
The state of the componentownProps
The same asReact Redux
's one
mapDispatchToProps(dispatch, dispatchToThis, [ownProps]) => dispatchProps
dispatch
The same asReact Redux
's onedispatchToThis
The function to dispatch an action to the component onlyownProps
The same asReact Redux
's one
mergeProps
and options
They will be passed to the React Redux's connect
directly.
component
A React component you want to connect to a Redux store and a component state.
componentReducer(componentState, action): newComponentState
A reducer to handle actions which is dispatched by dispatchToThis
from the component
and dispatch
from anywhere.
License
MIT