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

@4react/state

v2020.1.1

Published

A Redux-like state container for React Applications.

Downloads

8

Readme

@4react / state

A Redux-like state container for React Applications.

NOTE

This package is largely inspired by Redux. Please consider to support their work.

Usage

Import dependency

npm i @4react/state

Create Store

Use the createStore utility to create both the store provider and its corresponding consumer hook, passing the main reducer of the store to it.

import { createStore } from '@4react/state'

export const [ MyStore, useMyStore ] = createStore(myStoreReducer)

Provide Store

Use the store provider to provide the store down to your application.

import { MyStore } from 'myStore'

const App = () => {
  return (
    <MyStore>
      <MyComponent />
    </MyStore>
  )
}

Use store

Use the store custom hook to retrieve store data and dispatch actions

const MyInnerComponent = () => {
  const { data, dispatch } = useMyStore()

  const onClick = () => {
     dispatch({ type: 'YOUR_ACTION' })
  }

  return (
    <>
      <span>{data}</span>
      <button onClick={onClick}>
        click here
      </button>
    </>
  )
}

API

createStore()

Creates both a store provider and its corresponding hook.

Depending on the type of state management you want to implement, you can decide to keep a single global store, rather than creates multiple stores and provide them down to different sections of your application.

You can also use multiple nested stores for different manners.

createStore(reducer, [preloadedState], [enhancer])

| Param | Type | Default | Description | | --- | --- | --- | --- | | reducer | Function | - | A reducing function that returns the next state, given the current state and an action. | | preloadedState | any | computed | optional. The initial state. If not specify the initial state is automatically computed from reducers state default value. | | enhancer | Function | - | optional. The store enhancer. You may optionally specify it to enhance the store with third-party capabilities. |

Returns an array containing 2 different elements:

  1. The custom store provider (see Store Provider)
  2. The custom consumer hook. (see Consumer Hook)

Store Provider

The Store Provider is used to provide global store to the entire application. It is most commonly used inside app main component.

const App = () => {
  return (
    <MyStore>
      ...
    </MyStore>
  );
};

| Param | Type | Default | Description | | --- | --- | --- | --- | | data | any | - | optional You can use this prop to force the providing of specific store data. It is most commonly used for testing purpose. |

Consumer Hook

The Consumer Hook is used to retrieve both the actual store state and the dispatch function.

const { data, dispatch } = useMyStore()

const { data } = useMyStore(store => store.counter)

| Param | Type | Default | Description | | --- | --- | --- | --- | | selector | Function | - | optional Use to retrieve a custom section of the store state. |

| Return (object) | Type | Description | | --- | --- | --- | | data | any | The actual store state or a selection of it. | | dispatch | Function | The dispatch function, used to dispatch actions |

combineReducers()

Similar to Redux's combineReducers, it creates a new reducer starting from a map of reducers. The resulting function will be able to reduce a state with the same shape of the map of reducers passed to it.

const todos = combineReducers({
  all: allReducer,
  checked: checkedReducer
})

Arguments:

  1. reducers (object): the map of reducers to be combined together.
  2. [initialValue] (any): This parameter is optional. If not specify the state default value is automatically computed from reducers state default state value inside the passed map.

| Param | Type | Default | Description | | --- | --- | --- | --- | | reducers | object<string,Function> | - | The map of reducers to be combined together. | | initialValue | any | computed | optional If not specify, it is automatically computed from reducers default states value inside the passed map. |