Code Monkey home page Code Monkey logo

store4mp's Introduction

store4mp

A very basic data store manager library for mini program (mainly for cross-page usage).

Mini program's data share ability is very, very poor. There's not easy way to share data across pages. When it comes to nested pages, it will become harder.

store4mp provides a very basic & reliable way to share data between pages in component granularity.

Installation

npm install store4mp

Usage

Here's a demo that shows how it works.

componentStore.ts

import { createStoreManager } from "store4mp";

type Data = {
  value: string;
};

type Actions = {
  action: string; // action name & action payload
};

const { allocateStore, getStore, freeStore, getDefaultData } =
  createStoreManager<Data, Actions>();
export { allocateStore, getStore, freeStore, getDefaultData };

component/index.ts

Component({
  attached() {
    this.store = allocateStore();
    this.store.on("action", ({ payload, data }) => {
      console.log(payload, data); // string, { value: string }
    });
  },
  methods: {
    goToSecondaryPage() {
      this.store.data.value = "foo";
      wx.navigateTo(`url?component-store-id=${this.storeId}`);
    },
  },
  detached() {
    freeStore(this.store.id);
  },
});

Secondary page of component

Component({
  data: {
    ...getDefaultData(),
  },
  attached() {
    const currentPages = getCurrentPages();
    const currentPage = currentPages[currentPages.length - 1];
    const { options } = currentPage;

    this.store = getStore(options["component-store-id"]);
    this.setData(this.store.data);
    console.log(this.data); // { value: 'foo' }
  },
  methods: {
    handleTap() {
      this.store.dispatch("action", "hello main page");
      // parent page will show "hello main page"
    },
  },
});

About action & data

You may find you can pass data by dispatch(action) and store.data both, but which should be use?

There are two principles (which can cover most cases):

  • When you want pass data from parent page to child page, you should use store.data since child page doesn't exist when parent dispatch events.
  • Whan you want to pass data from child page to parent page, you should use dispatch(action). Since if parent listened to the action, there's no need to access store.data to acquire essential data.

There is no must be. You can choose proper method for your own scene.

Deep dive

It's eventbus + state store + resource management.

store4mp's People

Contributors

07akioni avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 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.