Giter VIP home page Giter VIP logo

mp-store's Introduction

mp-store

Build status Coverage NPM version QQ group

npm install @rustle/mp-store

一个轻量级的微信小程序状态管理库,他将能够平滑的兼容以前的旧项目,并不会有很强的侵入性,所以你可以直接迁移过来。如果你的小程序没有引入 npm 包的功能,可以在这里下载,然后放到项目中去。

Usage

这是一个简单的的迁移 demo

// 只需要两行代码,你就可以使用 mp-store 了
// 如果不想使用,也不会对你原有的项目产生影响
import createStore from '@rustle/mp-store'
const store = createStore()

App({
  // ...
})

下面是一个完整的使用 demo,更具体的使用,可以看相关部分的具体介绍

  1. 创建 reducer
store.add('action', {
  partialState: {
    name: 'tao',
  },
  setter (state, payload) {
    return { name: payload }
  }
})
  1. PageComponent 中使用
// mp-store 对 page 和 component 无差别对待
Page({
  storeConfig: {
    useState: (store) => ({
      name: state => state.name 
    })
  },

  onLoad () {
    this.data.global.name // tao
  },

  // 更改
  change () {
    this.store.dispatch('action', 'chentao')
    this.data.global.name // chentao
  },
})

如果你使用了模块,则会稍微有一点不同,看下面的 demo

  1. 创建 reducer
// 定义全局的 state
store.add('global', {
  partialState: {
    age: 25,
  },
})

// 模块
const action = 'action'
const reducer =  {
  partialState: {
    name: 'tao',
  },
  setter (state, payload) {
    return { name: payload }
  },
}

store.addMoudle('user', {
  [action]: reducer,
})
  1. PageComponent 中使用
// useState 是一个数组,第一个参数为需要注入的 module
Page({
  storeConfig: {
    useState: ['user', (store) => ({
      name: module => module.name,
      age: (module, rootState) => rootState.age
    })]
  },

  onLoad () {
    this.data.global.age // 25
    this.data.global.name // tao
  },

  // 更改
  change () {
    this.store.dispatch('action', 'chentao')
    this.data.global.name // chentao
  },
})

mp-store's People

Contributors

imtaotao avatar

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.