Skip to content

docs: store pattern #23

Open
Open

Description

@betula
// ./custom.store.ts

import { wrap, box, val } from "remini";

export const CustomStore = () => {
  const initialState = {
    address: {},
    user: {}
  };
  const $store = box(initialState);

  // example of performance optimization if store has a lot of often changes outside of "user" object
  const $user =  wrap(() => val($store).user);

  return Object.assign($store, {
    get user() {
      return val($user);
    },

    // this will be enought for most cases
    get address() {
      return val($store).address;
    },

    // ... some methods where
  })
}

And useful usage in everywhere in the code. React example:

// ./Custom.tsx

import { provide } from "provi";
import { useBox } from "remini/react";

const Custom = () => {
  const custom = useBox(provide(CustomStore));
  return (
    <>
      <b>{custom.user}</b>
      <address>{custom.address}</address>
    </>
  )
}

or without hooks:

// ./Custom.tsx

import { provide } from "provi";
import { component } from "remini";

const Custom = component(() => {
  const $custom = provide(Custom);
  return (
    <>
      <b>{$custom.user}</b>
      <address>{$custom.address}</address>
    </>
  )
})

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions