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

@octod/thestate

v0.3.1

Published

A tiny state manager writter in Rescript

Downloads

4

Readme

Thestate

Yet another global state manager.

Yes, it's done with push/sub

Yes, it's simple

Yes, it's made with Rescript

Why chosing it?

  • Simple, really simple
  • It's made with Rescript! ❤️
  • It's very tiny
  • Has .d.ts files for your js/ts apps
  • Built both for commonjs and es6 spec

install

yarn add @octod/thestate

If you are a js/ts dev, it's done. If you are using rescript (and I recommend it!), add the dependency to your bsconfig.json file

"bs-dependencies": [
  "@octod/thestate"
]

creating your first store (rescript)

You have four functions to know:

  • make, creates a store
  • getstate, returns a store's value
  • mutation, registers a mutation and returns a mutating function
  • listen, adds a listener to all mutations in a single store
let store = 100->Thestate.make
let increment = store->Thestate.mutation((state, payload) => state + payload)
let unsubscribelistener = store->Thestate.listen(state => Js.Console.log(state))

increment(100) // logs 200
store->Thestate.getstate->Js.Console.log // logs 200

unsubscribelistener()

increment(100) // does not log anymore, we have unsubscribed before

creating your first store (js/ts)

import * as thestate from 'thestate';

const store = thestate.make(100);
const increment = thestate.mutation(store, (state: number, payload: number) => state + payload);
const unsubscribelistener = thestate.listen(store, console.log);

increment(100) // logs 200
console.log(thestate.getstate(store)) // logs 200

unsubscribelistener()
increment(100) // does not log anymore, we have unsubscribed before

using it with react (rescript)

// create your store normally
let store = 100->Thestate.make
// maybe with some mutations is better
let increment = store->Thestate.mutation((state, payload) => state + payload)

module Increment = {
  @react.component
  let make = () => <button onClick={_ => increment(1)}> {"increment"->React.string} </button>
}

module Count = {
  @react.component 
  let make = () => {
    let count = store->Thestate.useState

    <React.Fragment>
      {`current state is ${count->Belt.Int.toString}`->React.string}
    </React.Fragment>
  }
}

module Counter = {
  @react.component
  let make = () => {
    <div>
      <Count />
      <Increment />
    </div>
  }
}

using it with react (js/ts)

import React, { useState, useEffect } from 'react';
import * as thestate from '@octod/thestate';

const store = thestate.make(100)
const increment = thestate.mutation(store, (a: number, b: number) => a + b);

const Increment = () => {
  return (
    <button onClick={() => increment(1)}>
      increment
    </button>
  )
}

const Count = () => {
  const count = thestate.useState(store)
  
  return (
    <>{`current state is ${count}`}</>
  )
}

const Counter = () => {
  return (
    <div>
      <Count />
      <Increment />
    </div>
  )
}

Contributions are really welcome

Any kind of contribution is really welcome, so don't be shy!