Code Monkey home page Code Monkey logo

react-minimalistic-store's Introduction

react-minimalistic-store

Simple wrapper for Context API

Get Started

Run npm install react-minimalistic-store to install.

Syntax

createStore

// Store creation
const {
  StoreProvider,
  useStore,
} = createStore<StoreType>(initialState);


// Get state
const { store, setState } = useStore();


// State mutation
function setState(
  newState: Partial<StoreType>,
  config: { mergeStrategy: "deep" | "shallow" | "replace" }
);
/*
 mergeStrategy is "deep" by default
 the "replace" merge strategy overwrites the entire state,
 only use it if every property of your state is optional

 newState would be partial of the store state
 for example, if your store looks like this:
*/
const initialState = {
  cart: [
    {
      name: "computer",
      price: 123,
    },
    {
      name: "console",
      price: 999,
    },
  ],
  user: {
    name: "Agus",
    profilePicture: "https://http.cat/418",
  },
};

// And you want to empty the "cart" property, then you can do this:
setState({ cart: [] });

// The resulting state would be:
const state = {
  cart: [],
  user: {
    name: "Agus",
    profilePicture: "https://http.cat/418",
  },
};

/*
 if you want to overwrite an entire property without merging it to its previous state
 (that would happen if the new state doesn't have properties that the previous did)
 then you can change the merge strategy:
*/
// "shallow" overwrites properties using a js spread, "deep" does a lodash deep merge
setState({ user: { name: "Agus" } }, { mergeStrategy: "shallow" });

// The resulting state would be:
const state = {
  cart: [],
  user: {
    name: "Agus",
  },
};

Code example

Store creation

// ./UserData.tsx

import createStore from "react-minimalistic-store";

export interface IUserData {
  user?: {
    username: string;
    profilePicture: string;
  };
}

const store = createStore<IUserData>({});

// Remember to use PascalCase for the provider, so react can read it
export const UserDataProvider = store.StoreProvider;

export const useUserData = store.useStore;

Wrapping app with provider

// ./index.tsx

import React from "react";
import ReactDOM from "react-dom";
import App from "App";
import { UserDataProvider } from "UserData";

ReactDOM.render(
  <UserDataProvider>
    <App />
  </UserDataProvider>,
  document.getElementById("root")
);

In app usage

// ./App.tsx

import { useUserData } from "UserData";

const App: React.FC = () => {
  // Destructure the state from store and rename the setState function
  // to prevent variable name collitions, in case you use this hook more than once in the same place
  const {
    store: { user },
    setState: setUserData,
  } = useUserData();

  return (
    <div>
      {user && (
        <div>
          <div>username: {user.username}</div>
          <img src={user.profilePicture} alt="profile" />
        </div>
      )}
      <input
        placeholder="username"
        onChange={(e) => {
          setUserData({ user: { username: e.target.value } });
        }}
      />
      <input
        placeholder="profilePicture"
        onChange={(e) => {
          setUserData({ user: { profilePicture: e.target.value } });
        }}
      />
    </div>
  );
};

export default App;

Warning

This library is not production tested, I use it for my own projects.

Use it at your own risk and please if you find a bug report it!

Thanks for your time! :]

react-minimalistic-store's People

Contributors

hex-spell avatar

Stargazers

 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.