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
函數呼叫時,監聽器將被刪除。
祝你好運!