Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Redux state like local component state

License

NotificationsYou must be signed in to change notification settings

esamattis/lean-redux

Repository files navigation

Redux state like local component state.

Design

  • Basic Redux state access and updating should be simple as it is with thecomponent local state
    • No need to manually create action types or reducers
    • The same API with React Components! Usethis.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 forparts of the app
  • Good performance
    • Lean Redux is build on top of the newconnectAdvanced() primitive ofReact Redux 5.0 and implements the same optimizations asconnect()
    • Handlers are automatically bound to avoid somepure render anti-patterns

Example

import{connectLean}from"lean-redux";varCounter=({count, handleClick})=>(<div>{count}<buttononClick={handleClick}>+1</button></div>);Counter=connectLean({getInitialState(){return{count:0};},handleClick(e){e.preventDefault();this.setState({count:this.state.count+1});},})(Counter);// Scope it to a myCounter key in the Redux state// <Counter scope="myCounter" />

To learn more checkout thelive examples.

Setup

yarn add lean-redux

or

npm install --save lean-redux

Just add theleanReducer to your store and start creating components withconnectLean.

import{createStore,applyMiddleware}from"redux";import{Provider}from"react-redux";import{leanReducer}from"lean-redux";conststore=createStore(leanReducer);varMain=()=>(<Providerstore={store}><Counter/></Provider>);ReactDOM.render(<Main/>,document.getElementById("app"));

If you already have other reducers you can mergeleanReducer into them withthecomposeReducers helper:

import{leanReducer,composeReducers}from"lean-redux";conststore=createStore(composeReducers(myReducer,myAnotherReducer,leanReducer));

Checkout theindex.js inexamplesfor complete example.

Usage with the ReduxcombineReducers helper

ThecombineReducers helper function does not like dynamically generated toplevel state keys so Lean Redux must be scoped under a specific key in the Reduxstate when used with thecombineReducers helper.

import{createStore,combineReducers}from"redux";import{leanReducer}from"lean-redux";leanReducer.setGlobalScope("lean");conststore=createStore(combineReducers({lean:leanReducer}));

API

Functions exported by thelean-redux module.

connectLean(options: Object): (component: ReactComponent) => ReactComponent

Connects a React component to a Redux store. Likeconnect() in React Redux itdoes not modify the component but returns a new one.

options

  • scope: string|Array|Function Scope the component to a part of the Reduxstate. Deep scopes can be defined with arrays. Missing path values in thestate will be automatically created as objects. If the value is a function itshould return the final scope. Parent component props are passed to thefunction. Ifscope is passed as a prop from the parent component it willoverride the value defined here unless it's a function.
  • getInitialState(): Object Create default values for the scoped state. LikeReact componentgetInitialState() this is executed only once when thecomponent is mounted.
  • mapState(state: Object, ownProps: Object): Object Modify the state beforepassing it to the wrapped component. Just like themapStateToProps in ReactRedux, but the state is scoped according to thescope option. If notdefined the default implementation is to return the props matching whatgetInitialState() returns.
  • defaultProps: Object Default props for the handler context.

Any other methods are considered to be "handlers" and are passed to the wrappedcomponent as props.

handlerContext

The context,this, in the handlers is like in the React Components.

  • this.state: Object The current scoped state from Redux
  • this.props: Object Props fromdefaultProps and any additional props passed bythe parent component.
  • this.setState(function|object nextState, [function callback]) Function toupdate the scoped Redux state. The API is exactly the same with the ReactComponentsetState().
  • this.dispatch: Function Redux store dispatch

About

Redux state like local component state

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp