Giter VIP home page Giter VIP logo

redux-weapp's Introduction

redux-weapp

Travis npm bundle size (minified + gzip) npm NpmVersion Known Vulnerabilities NpmLicense

Redux-based State Management for Wechat applet(微信小程序, weapp), to connect Redux store with your weapp's App or Page lifecycles.

Install

# via Github
npm install xixilive/redux-weapp --save-dev

# via npm
npm install redux-weapp --save-dev

# via yarn
yarn add -D redux-weapp

Build

git clone https://github.com/xixilive/redux-weapp.git \
  && cd redux-weapp \
  && yarn \
  && yarn build

Usage

Before trying these demo code snippets, you should/must be experienced in weapp modulize development. 微信小程序模块化开发实践

// Redux store
import {createStore} from 'redux'
//create your Redux store
const store = createStore(...)

connect to weapp App

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)

connect to weapp Page

Assume we have a store shape as following:

{
  "todos": [
    {
      "title": "String",
      "complete": "Boolean"
    }
  ]
}

and we have defined an action creator(FSA) as:

const fetchTodosAction = (status) => ({type: 'FETCH_TODOS', filter: {status}})

Ok, let's connect store to todo-list page.

// 
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)

connect API

connect.App

//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)

connect.Page

//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)

the onStateChange function

// would be called after each concerned state changed
onStateChange(nextState:Object, prevState:Object, initFlag:Any):void

Changes from v0.2.x

connect.App API

  • removed appLaunchOptions argument from mapState function.
// 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 API

  • removed pageLoadOptions argument from mapState function.
// 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 callback

  • add initFlag as the 3rd argument, which could be a string value INIT_SYNC just on the very first dispatch, undefined otherwise.
// v0.1.x
onStateChange(nextState:Object, prevState:Object)

// v0.2.x
onStateChange(nextState:Object, prevState:Object, initFlag:Any)

Lifecycles

for weapp App

  • onLaunch

setup an subscribe listener when onLaunch function called, and the initial store state will be broadcasted.

  • onShow

An inactive listener will be set to active when onShow function has called, and the listener will be called with last state.

  • onHide

An active listener will be set to inactive when onHide function has called.

for weapp Page

  • onLoad

setup an subscribe listener when onLoad function called, and the initial store state will be broadcasted.

  • onShow

An inactive listener will be set to active when onShow function has called, and the listener will be called with last state.

  • onHide

An active listener will be set to inactive when onHide function has called.

  • onUnload

The listener will be remove when onUnload function has called.


Good luck!

redux-weapp's People

Contributors

xixilive avatar yi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

redux-weapp's Issues

weapp 的 template 如何自我管理状态,有什么好办法吗?

weapp 的 template 如何自我管理状态,有什么好办法吗?

weapp 的 template 是一个单纯的渲染处理器。那么要实现根据用户交互的多级状态的组件,就比较麻烦了,因为组件的状态跟组件的渲染完全分离了。

对这样的情况,您有啥绝妙的主意吗?

方便留个微信或者QQ聊么?

我的微信 : good-day

请问如何在 Page 的 onShow 和 onHide 回调中访问到 通过 mapStateToProps 注入的对象?

请问如何在 Page 的 onShow 和 onHide 回调中访问到 通过 mapStateToProps 注入的对象?

下面是一个 Page 的代码,在注释里面,我想在 Page 的 onShow 方法中访问到 mapStateToProps 所注入的对象

rawPageData = {
  data: {
    PAGES: constants.PAGES,
  },
  onShow: function() {
    debugger;
    var baby;
    baby = this.props.baby;    // <-- 这里 this.props is undefined!
    console.log("[onLoad] baba:", baby);
  }
};

mapStateToProps = function(state) {
  console.log("state:", state);  //<-- 这里的state 包含 {acount, baby}
  return state;
};

mapActionToProps = function() {
  return {};
};

page = connect.Page(store, mapStateToProps, mapActionToProps)(rawPageData);

Page(page);

当 reducer 发生变化的时候, mapStateToProps 没有被再次执行,需要手动 setData?

下面是一个页面绑定的代码。请看注释中的问题:

var LINK_MAIN, connect, mapActionToProps, mapStateToProps, page, ref, store;

mapStateToProps = function(state) {
  return state;    // 当 reducer 发生变化的时候, 这里没有被再次执行,所以页面上的 data 数据还是旧的。应该怎么弄?
};

mapActionToProps = function() {
  return {};
};

page = connect.Page(store, mapStateToProps, mapActionToProps)({
  data: {
    LINK_MAIN: LINK_MAIN,
    account: store.getState().account
  },
  onStateChange: function(nextState) {
    console.log("onStateChange", nextState);
  }
});

Page(page);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.