Giter Site home page Giter Site logo

mobx-store-generator's Introduction

mobx-store-generator

Generates React Hooks and React Context for Mobx Stores. Includes Store persistance out of the box thanks to mobx-persist.

Usage

1. Create your store

import { HydrateStore, persist, GenerateStore } from "mobx-store-generator";

export type UserStoreProps = typeof StoreImpl;

class Store {
  @persist
  @observable
  public username?: string;

  @persist("object")
  @observable
  public funObject?: { name: string; titles: Array<string> };

  constructor() {
    this.username = "hello world!";
    this.funObject = {
      name: "Firaenix",
      titles: ["Software Developer"]
    };
  }
}
const StoreImpl = new Store();

// React Native
HydrateStore(AsyncStorage)("UserStore", StoreImpl);

// Web
HydrateStore(localStorage)("UserStore", StoreImpl);

export const UserStore = GenerateStore(StoreImpl);

2. Use your store in a Functional Component using hooks

import { observer } from 'mobx-react';
import { UserStore, UserStoreProps } from "./UserStore";

type HomePageProps = UserStoreProps;

export const HomePage = observer((props: HomePageProps) => {
  const userStore = UserStore.useStore();

  return <div>{userStore.username}</div>;
});

3. Use your store in a Class Component using Injection

import { UserStore, UserStoreProps } from "./UserStore";

type HomePageProps = UserStoreProps;

@UserStore.inject
export class HomePage extends React.Component<HomePageProps> {
  public render() {
    return <div>{this.props.username}</div>;
  }
}

4. Add your Provider to the Root of your application

Note: You only need to do this for a single store, if you have multiple, you dont need to do it again.

<UserStore.Provider>
  <App />
</UserStore.Provider>

Thanks

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.