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

use-gun-state

v1.0.3

Published

React hook that makes using gun.js for state management a dream.

Downloads

2

Readme

Use Gun State

A react hook wrapper for GUN with an api that mimics React.useState

Installation

yarn add use-gun-state

or

npm install --save use-gun-state

Usage

Initialising Gun

useGunState requires a GunContext at the root level of the react tree.

Gun doesn't seem to like being instantiated inside the react tree. So we have to instantiate a new IGunInstanceRoot at outside of the react tree and then pass it down to the GunContext so the useGunState hooks further down the tree can have access to it.

App.tsx;

const gun = Gun({
  peers: ["https://uri/to/peer:6789"],
  radisk: true,
  localStorage: true,
});

const root = ReactDOM.createRoot(
  document.getElementById("root") as HTMLElement
);

root.render(
  <React.StrictMode>
    <GunContext gun={gun}>
      <App />
    </GunContext>
  </React.StrictMode>
);

Setting State

The pattern of usage inside components is much the same as React.useState so it should feel reasonably familiar to any seasoned React developer.

There are three main differences in the way useGunState is works.

Difference 1 -> NodeId Not Initial State

In a React.useState we have to pass in a value to initialise the state with. But in useGunState we only pass in the nodeId? Why can't we initialise it with something? Well, gun is a decentralised graph database, so we want the initial data to be what ever is already in the graph. If we passsed in an initial state there is a chance we could override real data that's already waiting there for us :D

Difference 2 -> Nested Set State

The setState returned from useGunState is a little different than the deafualt react one because we are using valtio under the hood we get to make use of it's proxy magic! That means you can pass through a object that is Partial of your state type and only those values will be updated in the valtio store. (If you'd like more info about valtio check it out here https://github.com/pmndrs/valtio)

Difference 3 -> Live Updates

Because we are using gun we get real time updates for free! In the below example we have two people are looking at the same todo item, let's call them Alex and Blitz. When Blitz changes the title of the todo by typing into the input, Alex's title will update live as the changes are made. Wow, technology is crazy isn't it...

const [todo, setTodo] = useGunState({
  title: 'A Title',
  completed: false,
  assigned: 'Alex',
  collaborator: 'Blitz',
});

const onTitleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
 setState({
   title: e.target.value;
 });
}

const onCompletedChange = (e: React.ChangeEvent<HTMLInputElement>) => setState({ completed: e.target.checked })

return (
  <div className="Todo">
    <input type="text" value={todo.title} onChange={onTitleChange} />
    <input type="checkbox" value={todo.completed} onChange={onCompletedChange}
  </div>
)