Skip to content
This repository has been archived by the owner on Oct 31, 2018. It is now read-only.

Genuifx/wxa-redux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wxa-redux

Build Status NPM version codecov

redux for wxa.

Usage

  1. mount wxa-redux to wxa's instance.
// app.wxa app.js
import {wxa} from '@wxa/core'
import {wxaRedux, combineReducers} from '@wxa/redux'
import promiseMiddleware from 'redux-promise';

wxa.use(wxaRedux, {
    reducers: combineReducers(...your reducer),
    middlewares: [promiseMiddleware]
})
  1. add map to your page
// page.js
import {Page, wxa} from '@wxa/core'

@Page
class Index {
    mapState = {
        todolist : (state)=>state.todo
    }
    methods = {
        bindtap() {
            // dispatch your commit here
            this.store.dispatch({type: 'Add_todo_list', payload: 'coding today'});
            // and your page data will auto update.
        }
    }
} 

wxa.launchPage(Index)
  1. add map to your Component
// component.js
import {GetApp} from '@wxa/core'

// redux need mount app to com.
@GetApp
class Com {
    mapState = {
        todolist : (state)=>state.todo
    }
    methods = {
        bindtap() {
            // dispatch your commit here
            this.store.dispatch({type: 'Add_todo_list', payload: 'coding today'});
            // and your page data will auto update.
        }
    }
} 

wxa.launchComponent(Com);

Technical Detail

wxa-redux has different tasks to do according to current type. while in App layer, wr just create a store and mount store in the app. But in Page or Component layer, wr do a lot for users.

  1. App create store, apply middelware and mount store in app.

  2. Page we do different staff on essential life hook

  • onLoad wr subscribe to app.store and add a unsubscribe function to the instance.
  • onShow mark instance with $isCurrentPage, do once map state
  • onHide reset $isCurrentPage so that data will not update while page not at the front
  • onUnload unsubscribe change
  1. Component also doing little thing for com
  • created mount store
  • attached subscribe state change
  • detached unsubscribe

Releases

No releases published

Packages

No packages published