npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@apikee/react-simple-store

v2.0.1

Published

Simple and easy to use global store for React

Downloads

19

Readme

react-simple-store

Simple and easy to use global store for React

Usage:

  1. Create store
import { simpleStore } from "@apikee/react-simple-store";

const store = simpleStore({
  name: "John",
  age: 32,
});

With Typescript:

import { simpleStore } from "@apikee/react-simple-store";

interface User {
  name: string;
  age: number;
}

const store = simpleStore<User>({
  name: "John",
  age: 32,
});
  1. Use store data in components
const MyComponent = () => {
  const data = store.useStore();

  return <div>{data.name}</div>;
};
  1. Change store values from inside a component
// ._set() method is a part of the data object, you can use it to change the store data
<button onClick={() => data._set({ age: Math.floor(Math.random() * 60) })}>
  Change Age
</button>

Or outside of component

const store = simpleStore({
  name: "John",
  age: 32,
});

store.set({ age: Math.floor(Math.random() * 60) });
// Components gets re-rendered accordingly
  1. Get store values outside of components
// Note that if used in component this way, React will not re-render component when value changes
// Always use store.useStore() in components to get the store values
const { name, age } = store.get();
  1. If needed, create computed properties from several separate stores through custom hooks
const store1 = simpleStore({ firstName: "John" });
const store2 = simpleStore({ lastName: "Doe" });

const useFullName = () => {
  const { firstName } = store1.useStore();
  const { lastName } = store2.useStore();

  return `${firstName} ${lastName}`;
};

const MyComponent = () => {
  // Component gets re-rendered if firstName or lastName changes
  const fullName = useFullName();

  ...
}

Example:

import React, { useEffect, useState } from "react";
import { simpleStore } from "@apikee/react-simple-store";

const defaultValue = {
  name: "John",
  age: 32,
};

// Default value has to be object
const store = simpleStore(defaultValue);

const ComponentOne = () => {
  const data = store.useStore();

  useEffect(() => {
    console.log("Name Changed");
  }, [data.name]);

  return (
    <>
      <div>My name is {data.name}</div>

      {/* Changing `age` property that is used in different component */}
      <button
        onClick={() => data._set({ age: Math.floor(Math.random() * 60) })}
      >
        Change Age
      </button>
    </>
  );
};

const ComponentTwo = () => {
  const data = store.useStore();

  const [newName, setNewName] = useState("");

  useEffect(() => {
    console.log("Age Changed");
  }, [data.age]);

  return (
    <div>
      <div>My age is {data.age}</div>
      <input value={newName} onChange={(e) => setNewName(e.target.value)} />

      {/* Changing `name` property that is used in different component */}
      <button onClick={() => data._set({ name: newName })}>Change Name</button>
    </div>
  );
};

const App = () => {
  return (
    <div>
      <ComponentOne />
      <ComponentTwo />
    </div>
  );
};