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

tinyrx

v1.0.2

Published

a tiny library to extend reacts reduce hooks

Downloads

1

Readme

TinyRX (react-redux)

npm version test Known Vulnerabilities build passing License: MIT Sourcegraph

:pill: TinyRX is an interface library to add middleware and React-Redux like reducing to Reacts hook system.

What does tinyRX do

TinyRX adds the missing features into reacts current useReducer landscape

Adds:

  • Combine Reducers
  • useMiddleWare

Use

Adding TinyRX to Your Project is simple!


import {useMiddleWare, combineReducers} from 'tinyrx';

TinyRX no longer provides context, its much easier to create your own.

Setup

TinyRX requires a minimal setup compared to other libraries.

Define Your reducers
const todoReducer = (state, action) => {
  switch(action.type) {
    case 'ADD_TODO': return [...state, action.payload];
    default: return state;
  }
}

const bookReducer = (state, action) => {
  switch(action.type) {
    case 'ADD_BOOK': return [...state, action.payload]
    default: return state;
  }
}
Setup the top level App

Note that the name of the keys in combine reducers must match the initial state passed to useReducer.

// combine reducers combines both Todos and Books together
const rootReducer = combineReducers({Todos: todoReducer, Books: bookReducer});
// warning just like with redux if both reducers have the same Key or Type both will be affected
// create context using react
const AppContext = React.createContext(null);

function App () {
  const [state, dispatch] = useReducer(rootReducer, {Books: [], Todos: []});
  return (
    <div>
      <AppContext.Provider value={{state, dispatch}}>
        <TodoComponent />
      </AppContext.Provider>
    </div>
  )
}

Define a component to consume the Context

function TodoComponent() {
  // useContext provides state and the dispatch function to your components
  const {state, dispatch} = useContext(AppContext);
  const [input, setInput] = useState("");
  function AddTodo(e){
    dispatch({type: 'ADD_TODO', payload: input})
  }

  function UpdateInput(e) {
    e.persist();
    setInput(input => e.target.value);
  }
  return (
  <div>
    <ul>
    {state.Todos.map(item => <li>{item}</li>)}
    </ul>
    <input onChange={UpdateInput}></input>
    <button onClick={AddTodo}>Click me</button>
  </div>
  )
}

MiddleWare

Using middleware is trivial! Just pass in an array of middleware you want to use to the new useMiddleware function! The dispatch passed to context must be changed to use the function returned from useMiddleWare; heres an example with thunk!

function App () {
  const AppContext = context;
  const [state, dispatch] = useReducer(Reducers, {Loaded: false});

  return (
    <div>
      <AppContext.Provider value={useMiddleWare([thunk], { state, dispatch })}>
        <Loader />
      </AppContext.Provider>
    </div>
  )
}

function Loader() {
  const { state, dispatch } = useContext(context);
  const testAsync = e => {
    dispatch(fetchPosts());
  };
  return (
    <div>
      {!state.Loaded ? <p>loaded</p> : <p>...loading</p>}
      <button onClick={testAsync}>Test</button>
    </div>
  );
}

function fetchPosts() {
  return function(dispatch, getState) {
    dispatch("IS_LOADING");
    return fetch('https://jsonplaceholder.typicode.com/todos/1')
      .then(
        response => response.json(),
        error => console.log('An error occurred.', error)
      )
      .then(json =>{
        dispatch("IS_LOADED");
        return json;
      }
      )
  }
}

Depends

react: ^16