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-context-tk

v1.1.1

Published

React Context API as @reduxjs/toolkit, use only react app

Downloads

2

Readme

Description

This package is for using react context with functions similar to those in rudexjs/toolkit.

Install

npm i react-context-tk

Example

store.ts

import { Store, TSliceAction, createSlice, TMiddleware } from 'react-context-tk';

const initAppState = {
  count: 0,
  test: '',
  isLoading: false,
};
const initOtherState = {
  text: 'test',
  test: {
    test1: {
      test2: {
        text: 'test2',
        test3: {
          result: {
            text: 'result',
          },
        },
      },
    },
  },
};

type TAppState = typeof initAppState;
type TOtherState = typeof initOtherState;
// ---> v1 action example
const onCount: TSliceAction<TAppState, number> = (state, payload) => {
  state.count = payload;
};
// ---> v2 action example
const onChangeText = (state: TOtherState, payload: Pick<TOtherState, 'text'>) => {
  state.text = payload.text;
};

const appSlice = createSlice({
  name: 'app',
  initState: initAppState,
  reducers: {
    onCount,
    // ---> v3 action example
    test(state, payload: number) {
      console.log(state, payload);
    },
    onFetching(state, payload: boolean) {
      state.isLoading = payload;
    },
  },
});

const otherSlice = createSlice({
  name: 'test',
  initState: initOtherState,
  reducers: {
    onChangeText,
  },
});

const store = {
  ...appSlice.sliceStore,
  ...otherSlice.sliceStore,
};

const actions = {
  [appSlice.name]: appSlice.actions,
  [otherSlice.name]: otherSlice.actions,
};

export const { useStore, StoreProvider, storeInstance } = Store(store, actions);

const actionMiddleware: TMiddleware<typeof storeInstance> = async (props) => {
  switch (props.action.type) {
    case 'app/onCount':
      props.dispatch(props.actions.app.onFetching(true));
      await fetch('https://swapi.dev/api/');
      props.dispatch(props.actions.app.onFetching(false));
  }
};

const middlewares = storeInstance.createMiddleware(actionMiddleware);

console.log(middlewares, storeInstance);

app.tsx

import React from 'react';
import { StoreProvider, useStore } from './store';

export const Counter = () => {
  const [[count, text, store], { dispatch, actions }] = useStore((state) => [state.app.count, state.test.text, state]);

  console.log('store', store);

  return (
    <div>
      <div
        style={{
          pointerEvents: store.app.isLoading ? 'none' : 'auto',
          opacity: store.app.isLoading ? 0.5 : 1,
        }}>
        <button
          style={{ marginRight: 10, width: 50 }}
          onClick={() => dispatch(actions.app.onCount(count - 1))}
          children=" - "
        />
        <button
          style={{ marginRight: 10, width: 50 }}
          onClick={() => dispatch(actions.app.onCount(count + 1))}
          children=" + "
        />
        <div>{count}</div>
      </div>
      <input
        value={text}
        onChange={(e) =>
          dispatch(
            actions.test.onChangeText({
              text: e.target.value,
            })
          )
        }
      />
    </div>
  );
};

export const App = () => {
  return (
    <StoreProvider>
      <Counter />
    </StoreProvider>
  );
};