React Redux Ready
React-Redux-Ready provides a simpler way to use Redux in React powered apps.
Installation
npm install --save react-redux-ready
How to Use
Application entry file
You need to do two things here:
- Import the store from 'react-redux-ready' then pass it to the Provider component
- Call updateRootReducer right before calling the render method
;;;;; const App = <Provider store=store> <Main /> </Provider>; // you must call updateRootReducer before rendering your app; // render your app;
Component file
Here is what you need to do in a component file:
- Import the connect function from 'react-redux-ready'
- Import the reducer and all action creators related to this component
- Pass them all to the connect function and export the returned component
;;;;; const MainPage = <div> <button onClick= console> I will consolelog the props object when I am clicked </button> <button onClick= console> I will consolelog the actions object when I am clicked </button> </div>; MainPagepropTypes = myStateKey: PropTypesobjectisRequired actions: PropTypesobjectisRequired; MainPage reducer actions stateKey: 'myStateKey' actionsKey: 'myActionsKey' ;
Component state can then be accessed via this.props.myStateKey
and component actions can be accessed via this.props.actions.myActionsKey
, this is to avoid naming conflicts between different state keys, actions and component defined properties.
Object destructuring
You can use object destructuring to extract state and actions and avoid writing long lines of code.
For a component with the following configuration:
SignupPage reducer actions stateKey: 'signup' actionsKey: 'signup' ;
You would do the following:
{ const username email = thispropssignup; const login resetPassword = thispropsactionssignup; return <div> <h1> Your username: username Your e-mail: email </h1> <button onClick=login> Login </button> <button onClick=resetPassword> Forgot your password? </button> </div> );}
The connect method parameters
component (required)
React component reference
configObject (optional)
Configuration object that contains the following keys:
- reducer: Reference to the component reducer.
- actions: Reference to the actions object.
- stateKey: Namespace that will be used to pass component state as props.
- actionsKey: Namespace that will be used to pass component actions as props.
Middlewares
To use a middleware, import useMiddleware method and pass it the middleware, here is an example using React Router (v3.0.5):
;;;;;;; // register router reducer; // create the routing middleware; // create historyconst history = ; const MainApp = <Provider store=store> <Router history=history key=Math> <Route path="/" component=Main> ... </Route> </Router> </Provider>; ; // render everything;