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

react-tini

v1.0.0

Published

Tiny but powerful state management library

Downloads

3

Readme

react-tini

Tiny but powerful state management library

Counter App

import React from "react";
import { render } from "react-dom";
import T from "react-tini";

const Increase = () => {
  return {
    counter: current => current + 1
  };
};

const App = T((props, context) => {
  const counter = context.get(state => state.counter);

  function handleClick() {
    context.dispatch(Increase);
  }

  return (
    <>
      <h1>{counter}</h1>
      <button onClick={handleClick}>Increase</button>
    </>
  );
});

render(<App />, document.getElementById("root"));

Counter App (Compact version)

import React from "react";
import { render } from "react-dom";
import T from "react-tini";

const Increase = () => ({
  counter: current => current + 1
});

const App = T((props, { get, dispatch }) => (
  <>
    <h1>{get("counter")}</h1>
    <button onClick={() => dispatch(Increase)}>Increase</button>
  </>
));

render(<App />, document.getElementById("root"));

Counter App (Using state, action mappings)

import React from "react";
import { render } from "react-dom";
import T from "react-tini";

const Increase = () => ({
  counter: current => current + 1
});

const App = T(
  {
    states: {
      counter: state => state.counter
    },
    actions: {
      increase: () => [Increase]
    }
  },
  ({ counter, increase }) => (
    <>
      <h1>{counter}</h1>
      <button onClick={increase}>Increase</button>
    </>
  )
);

render(<App />, document.getElementById("root"));

Simple action

// action returns multiple state reducers
const Increase = () => ({
  counter: current => current + 1
});

// action returns single state reducer
const Decrease = () => state => ({
  ...state,
  counter: state.counter - 1
});

Dispatching action from other action

const Increase = () => ({
  counter: current => current + 1
});

const IncreaseDouble = dispatch => {
  dispatch(Increase);
  dispatch(Increase);
};

Async Action

const Fetch = async (dispatch, url, onSuccess, onFailure) => {
  try {
    const res = await fetch(url);
    const json = await res.json();
    onSuccess && dispatch(onSuccess, json);
  } catch (e) {
    onFailure && dispatch(onFailure, e);
  }
};

const ProductLoaded = (_, products) => ({
  products: () => products
});

const LoadProduct = dispatch => {
  dispatch(Fetch, "http://", ProductLoaded);
};

Auto dispatch action

import T from "react-tini";

const LoadProduct = (dispatch, category, filter) => {};

const ProductList = T(
  {
    dispatch: () => [LoadProduct]
  },
  () => null
);

// passing component props to action
const ProductListByCategory = T(
  {
    dispatch: ({ props }) => [LoadProduct, props.category]
  },
  () => null
);

// passing state to action
const ProductListByCategoryAndFilter = T(
  {
    dispatch: ({ props, state }) => [LoadProduct, props.category, state.filter]
  },
  () => null
);

Remark: An action will re-dispatch once its input arguments changed

Using T as HOC

import React from "react";
import T from "react-tini";

const container = T.compose(
  withSomething1(options),
  withSomething2(options),
  T(options)
);

const wrappedComponent = container(component);

Using T.hoc()

const ContainerA = hoc(props => {
  return {
    ...props,
    extraProp: true
  };
});

const ContainerB = hoc((props, Comp) => {
  return (
    <>
      <Comp {...props} extraProp={true} />
      <ExtraComponent />
    </>
  );
});

Handling state change

import T from "react-tini";

T.subscribe(action => {
  console.log("action ", action.name, " is dispatched");
});

Accessing state

import T from "react-tini";

// whole state
console.log(T.get());

// piece of state
console.log(T.get(state => state.counter));