JSPM

th12storage

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12
  • Score
    100M100P100Q26164F
  • License ISC

React State Manager

Package Exports

  • th12storage
  • th12storage/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (th12storage) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

th12storage

A small global state manager in React.

How to use

    import { useStorage } from "th12storage";

    function Test(){
        let [hello] = useStorage("hello", "Hello");
    }

To be able to change the value, you have to create a method to change it.

    import { useStorage } from "th12storage";

    function Test(){
        let [hello, setHello] = useStorage("hello", "Hello", true);

        setHello("Hello, world!");
    }

But even with this change, the component will not respond to it. You need to create function trigger.

    import { useState } from "react";
    import { useStorage } from "th12storage";

    function Test(){
        let [st, rst] = useState(0);
        let [hello, setHello] = useStorage("hello", "Hello", true);

        if(hello==="Hello"){
            setHello("Hello, world!", rst);
        }
    }

This method is not convenient if you use a lot of variables. In that case, you can use subscribes.

    import { useState } from "react";
    import { useStorage, useStorageSubscribe } from "th12storage";

    function Test(){
        let [st, rst] = useState(0);
        useStorageSubscribe("Test", ["hello", "user"], rst);

        let [hello, setHello] = useStorage("hello", "Hello", true);
        let [user, setUser] = useStorage("user", "", true);

        if(user===""){
            setUser("John Doe");
        }else if(hello=="Hello"){
            setHello("Hello, "+user+"!");
        }
    }

Parameters

useStorage(VARIABLE NAME, DEFAULT VALUE [,CREATE UPDATE VARIABLE TRIGGER])

useStorageSubscribe(COMPONENT NAME, SUBSCRIBES LIST, COMPONENT UPDATE TRIGGER)