Redux state like local component state
npm install lean-reduxRedux state like local component state.
- Basic Redux state access and updating should be simple as it is with the
component local state
- No need to manually create action types or reducers
- The same API with React Components! Use this.setState() to update Redux state
- Redux state is be scoped to the components
- Component cannot interfere with parts of the state that do not belong to it
- Play well with other tools in the Redux community
- Time travel debuggers, state serialization tools, Redux Form etc. work well with Lean Redux
- You can drop this into your existing project and start using it only for
parts of the app
- Good performance
- Lean Redux is build on top of the new connectAdvanced() primitive of
React Redux 5.0 and implements the same optimizations as connect()
- Handlers are automatically bound to avoid some pure render anti-patterns
``js
import {connectLean} from "lean-redux";
var Counter = ({count, handleClick}) => (
// Scope it to a myCounter key in the Redux state
//
`
To learn more checkout the live examples.
npm install --save lean-redux
Just add the leanReducer to your store and start creating components withconnectLean.
`js
import {createStore, applyMiddleware} from "redux";
import {Provider} from "react-redux";
import {leanReducer} from "lean-redux";
const store = createStore(leanReducer);
var Main = () => (
);
ReactDOM.render(, document.getElementById("app"));
`
If you already have other reducers you can merge leanReducer into them withcomposeReducers
the helper:
`js
import {leanReducer, composeReducers} from "lean-redux";
const store = createStore(composeReducers(myReducer, myAnotherReducer, leanReducer));
`
Checkout the index.js in
examples
for complete example.
The combineReducers helper function does not like dynamically generated topcombineReducers
level state keys so Lean Redux must be scoped under a specific key in the Redux
state when used with the helper.
`js
import {createStore, combineReducers} from "redux";
import {leanReducer} from "lean-redux";
leanReducer.setGlobalScope("lean");
const store = createStore(combineReducers({
lean: leanReducer
}));
`
Functions exported by the lean-redux module.
Connects a React component to a Redux store. Like connect() in React Redux it
does not modify the component but returns a new one.
#### options
- scope: string|Array|Function Scope the component to a part of the Reduxscope
state. Deep scopes can be defined with arrays. Missing path values in the
state will be automatically created as objects. If the value is a function it
should return the final scope. Parent component props are passed to the
function. If is passed as a prop from the parent component it willgetInitialState(): Object
override the value defined here unless it's a function.
- Create default values for the scoped state. LikegetInitialState()
React component this is executed only once when themapState(state: Object, ownProps: Object): Object
component is mounted.
- Modify the state beforemapStateToProps
passing it to the wrapped component. Just like the in Reactscope
Redux, but the state is scoped according to the option. If notgetInitialState()
defined the default implementation is to return the props matching what
returns.defaultProps: Object
- Default props for the handler context.
Any other methods are considered to be "handlers" and are passed to the wrapped
component as props.
#### handlerContext
The context, this, in the handlers is like in the React Components.
- this.state: Object The current scoped state from Reduxthis.props: Object
- Props from defaultProps and any additional props passed bythis.setState(function|object nextState, [function callback])
the parent component.
- Function tosetState()
update the scoped Redux state. The API is exactly the same with the React
Component .this.dispatch: Function` Redux store dispatch
-