Coder Social home page Coder Social logo

wx-redux-1's Introduction

wx-redux 微信小程序状态管理工具

小程序接入redux,实际使用代码参见:Demo

一.使用步骤

1.将此工程/dist下的 redux.js,wx-redux.js文件复制到你的/lib下 2.编写reduce文件,比如下面的例子

import { combineReducers } from '../lib/redux';

const title = (state='title', action) => {
  switch (action.type) {
    case 'UPDATE_TITLE':
      return action.payLoad;
    default:
      return state
  }
};

const app = combineReducers({
  title,
});

export default app

3.在app.js中引入store

import './lib/wx-redux';
import { createStore } from './lib/redux';
import reducers from "./reducers/index";

const store = createStore(reducers);

App({
  store,
});

4.编写action

# /actions/index.js
export const updateTitle=(title) => ({
  type: 'UPDATE_TITLE',
  payLoad: title,
});

5.在page的代码中引入你要使用state以及action

# 引入action文件
import {updateTitle} from '../../actions/index';
Page({
  data:{
    a:'aaa',
  },
  actions:[updateTitle],  # 引入需要直接调用的action
  states:['title'], # 引入需要直接使用的state中的值
  onLoad(){},
  bindKeyInput(e){
    const text =e.detail.value;
    this.updateTitle(text);   # 直接调用action来更新store
  }
});

6.直接调用action来更新store

bindKeyInput(e){
    const text =e.detail.value;
    this.updateTitle(text);
  }

原理

wx-redux-1's People

Contributors

18boys 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.