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

blackbox.js

v1.0.3

Published

Blackbox.js is a library for handling state inside your React application. ๐Ÿ“ฆ

Downloads

7

Readme

Blackbox.js

Blackbox is a predictable, boilerplate free state management library for React. ๐Ÿ“ฆ

It's designed specifically for React applications, so you don't have to worry about installing other third party packages.

And it's tiny! ๐Ÿค

Index

  1. Blackbox
  2. Index
  3. Installation
  4. Learn
    1. The gist
    2. React
  5. Examples
  6. License
  7. Author

Installation

Assuming your using npm as your package manager, install it typing the command below in your terminal:

$ npm i blackbox.js

Learn ๐Ÿค“

Blackbox is a library designed for you to deal with globally shared state as easily as possible, without introducing any boiler plate code like reducers, switch statements or providers. With it, you're encouraged to do things as simple as possible.

Similar to other flux-like libraries, it's architecture is based on the single source of truth principle, where the state is a single global instance in your app, making it easier to reason about where the state and actions are coming from.

The gist ๐Ÿง 

As said previously, blackbox is designed to give you a simple and boilerplate free API for dealing with your state. All you have to do is to create a globally available box object to share between your components.

The example below shows the usage of it with a simple counter app.

import createBox from 'blackbox.js'

// Creates a reactive box object that'll
// update and trigger any subscribers.
const counterBox = createBox(0)

/**
 * This is an action, an action is the preferred way to
 * interact with your state.
 *
 * We define them as functions that when triggered,
 * updated the internal state of a box.
 *
 * In this case, then the function "increment" is called we
 * increment the counter by the parameter "incrementBy".
 *
 * Declaring well defined actions will make your life easier
 * in the long run, since everything will be mapped to a
 * function. ๐Ÿ˜ธ
 *
 */
function increment(incrementBy = 1) {
  counterBox.set((counter) => counter + incrementBy)
}

/**
 * Since we've defined an action to increment the counter previously, the
 * "decrement" can call the "increment" with a negative value.
 *
 * In this function you can see that actions can call other actions or either
 * call themselves!
 */
function decrement() {
  increment(-1)
}

counterBox.subscribe(console.log) // This will log any changes in the internal box state for us.

/**
 * As seen below, both calls are independent of the component
 * context and don't need a hook to be triggered.
 *
 * We'll see how it's used inside a component in the section below.
 */
increment() // Increments the counter
decrement() // Decrements the counter

React โš›๏ธ

Building on the previous example, blackbox provides you with hooks you can use to flatten a box object to it's original state and make it reactive. We'll build a counter app with the previous actions to show you how easy it is.

import React from 'react'
import createBox, { useBox } from 'blackbox.js'

// As shown in the previous example, you can
// create a box from any value. We are using
// zero as the initial state for the box.
const counterBox = useBox(0)

/**
 * Our user-defined action to increment the
 * counter by a value (which defaults to one).
 */
function increment(incrementBy = 1) {
  counterBox.set((counter) => counter + incrementBy)
}

/**
 * Another action to decrement the counter by one.
 */
function decrement() {
  increment(-1)
}

/**
 * In here, we're using the "useBox" hook to flatten the
 * box object to it's original value (zero).
 *
 * This hook makes the counter reactive to triggered actions
 * and deals automatically with the subscription management
 * and cleanup.
 */
export default function App() {
  const counter = useBox(counterBox) // This will flatten the box to the zero value.

  return (
    <div>
      <h1>Counter is: {counter}</h1>
      <div>
        <button onClick={() => increment()}>Increment</button>
        <button onClick={() => decrement()}>Decrement</button>
      </div>
    </div>
  )
}

Examples ๐Ÿ“”

License ๐Ÿ’ผ

MIT

Author

| Eder Lima | | ------------------------------------------------------ | | Eder Lima |