redux weapp
1.0.0
基于 Redux 的微信小程序状态管理,将 Redux store 与你的 weapp 的 App 或 Page 生命周期连接起来。
# via Github
npm install xixilive/redux-weapp --save-dev
# via npm
npm install redux-weapp --save-dev
# via yarn
yarn add -D redux-weapp
git clone https://github.com/xixilive/redux-weapp.git
&& cd redux-weapp
&& yarn
&& yarn build
在尝试这些演示代码片段之前,您应该/必须具有 weapp 模块化开发经验。 微信小程序优先开发实践
// Redux store
import { createStore } from 'redux'
//create your Redux store
const store = createStore ( ... )
import connect from 'redux-weapp'
const config = connect . App (
store ,
//to map next state into your app
( state ) => {
// must return an object, which will be passed to onStateChange function
return { }
} ,
// to bind dispatch with your action,
// and this binding will be injected into your app object.
( dispatch ) => {
// return an object, which can be invoked within app context(this scope).
return { }
}
) ( {
onLaunch ( options ) { } ,
//...,
onStateChange ( nextState , prevState , initFlag ) {
this . setData ( { ... this . data , ... nextState } )
}
} )
// construct your app
App ( config )
假设我们有一个商店形状如下:
{
"todos" : [
{
"title" : " String " ,
"complete" : " Boolean "
}
]
}
我们将动作创建者(FSA)定义为:
const fetchTodosAction = ( status ) => ( { type : 'FETCH_TODOS' , filter : { status } } )
好的,让我们将商店连接到待办事项列表页面。
//
import connect from 'redux-weapp'
const config = connect . Page (
store ,
//to map next-state into your page
( state ) => ( { todos : state . todos } ) ,
// to bind dispatch with your action creators,
// and this binding will be injected into your page object
( dispatch ) => ( {
fetchTodos ( status = 'inprogress' ) {
// dispatch an action
dispatch ( fetchTodosAction ( status ) )
}
} )
) ( {
onLoad ( options ) {
this . fetchTodos ( 'inprogress' )
} ,
onStateChange ( nextState , prevState , initFlag ) {
const { todos } = nextState
this . setData ( { todos } ) // to update UI
} ,
// view interactions
onTapCompleteTab ( ) {
this . fetchTodos ( 'complete' )
} ,
onTapInProgressTab ( ) {
this . fetchTodos ( 'inprogress' )
}
} )
// construct your page
Page ( config )
//define app connect function
factory = connect . App (
store : ReduxStore ,
mapStateToProps : Function ( state : Object ) ,
mapDispatchToProps : Function ( dispatch : Function )
) : Function
//build a store-binding app config object
config = factory ( {
onLaunch ( options : Object ) { } ,
onStateChange ( nextState : Object , prevState : Object , initFlag : Any ) ,
//...
} ) : Object
//launch app with store-binding config
App ( config )
//define page connect function
factory = connect . Page (
store : ReduxStore ,
mapStateToProps : Function ( state : Object ) ,
mapDispatchToProps : Function ( dispatch : Function )
) : Function
//build a store-binding page config object
config = factory ( {
onLoad ( options : Object ) { } ,
onStateChange ( nextState : Object , prevState : Object , initFlag : Any )
//...
} ) : Object
//start page instance with store-binding config
Page ( config )
onStateChange
函数 // would be called after each concerned state changed
onStateChange ( nextState : Object , prevState : Object , initFlag : Any ) : void
initFlag
:来自[email protected]
的新功能,有关详细信息,请参阅“更改”部分。 connect.App
APImapState
函数中删除了appLaunchOptions
参数。 // v0.1.x
connect . App ( {
store : ReduxStore ,
mapState : Function ( state : Object , appLaunchOptions : Object ) : Object ,
mapDispatch : Function ( dispatch : Function ) : Object ,
} )
// v0.2.x
connect . App ( {
store : ReduxStore ,
mapState : Function ( state : Object ) : Object ,
mapDispatch : Function ( dispatch : Function ) : Object ,
} )
connect.Page
APImapState
函数中删除了pageLoadOptions
参数。 // v0.1.x
connect . Page ( {
store : ReduxStore ,
mapState : Function ( state : Object , pageLoadOptions : Object ) : Object ,
mapDispatch : Function ( dispatch : Function ) : Object ,
} )
// v0.2.x
connect . Page ( {
store : ReduxStore ,
mapState : Function ( state : Object ) : Object ,
mapDispatch : Function ( dispatch : Function ) : Object ,
} )
onStateChange
回调initFlag
作为第三个参数,它可以是第一次调度时的字符串值INIT_SYNC
,否则undefined
。 // v0.1.x
onStateChange ( nextState : Object , prevState : Object )
// v0.2.x
onStateChange ( nextState : Object , prevState : Object , initFlag : Any )
onLaunch
当onLaunch
函数调用时设置订阅监听器,并且初始存储状态将被广播。
onShow
当调用onShow
函数时,非活动侦听器将被设置为active
,并且侦听器将以最后状态调用。
onHide
当onHide
函数调用时,活动监听器将被设置为非inactive
。
onLoad
当onLoad
函数调用时设置一个订阅监听器,并且初始存储状态将被广播。
onShow
当调用onShow
函数时,非活动侦听器将被设置为active
,并且侦听器将以最后状态调用。
onHide
当onHide
函数调用时,活动监听器将被设置为非inactive
。
onUnload
当onUnload
函数调用时,监听器将被删除。
祝你好运!