All Projects → kqito → Use Global Context

kqito / Use Global Context

Licence: mit
A new way to use “useContext” better

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Use Global Context

Yewdux
Redux-like state containers for Yew apps
Stars: ✭ 58 (+70.59%)
Mutual labels:  context, state-management, state
useSharedState
useSharedState is a simple hook that can be used to share state between multiple React components.
Stars: ✭ 0 (-100%)
Mutual labels:  hooks, state-management, state
React Workshop
⚒ 🚧 This is a workshop for learning how to build React Applications
Stars: ✭ 114 (+235.29%)
Mutual labels:  context, state-management, state
Alveron
Elm & Reason inspired state management for React
Stars: ✭ 57 (+67.65%)
Mutual labels:  context, state-management, state
eventrix
Open-source, Predictable, Scaling JavaScript library for state managing and centralizing application global state. State manage system for react apps.
Stars: ✭ 35 (+2.94%)
Mutual labels:  hooks, state-management, state
Contextism
😍 Use React Context better.
Stars: ✭ 141 (+314.71%)
Mutual labels:  context, state-management, state
Pure Store
A tiny immutable store with type safety.
Stars: ✭ 133 (+291.18%)
Mutual labels:  hooks, state-management, state
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+623.53%)
Mutual labels:  hooks, state-management, state
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-61.76%)
Mutual labels:  hooks, state-management, state
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (+91.18%)
Mutual labels:  hooks, state-management, context
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+58.82%)
Mutual labels:  hooks, state-management, state
react-wisteria
Managing the State with the Golden Path
Stars: ✭ 18 (-47.06%)
Mutual labels:  state-management, state, context
Pullstate
Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!
Stars: ✭ 683 (+1908.82%)
Mutual labels:  hooks, state-management, state
Revived
A redux-inspired predictable state container for python
Stars: ✭ 12 (-64.71%)
Mutual labels:  state-management, state
React Recomponent
🥫 Reason-style reducer components for React using ES6 classes.
Stars: ✭ 272 (+700%)
Mutual labels:  state-management, state
Radioactive State
☢ Make Your React App Truly Reactive!
Stars: ✭ 273 (+702.94%)
Mutual labels:  hooks, state-management
Effector
The state manager ☄️
Stars: ✭ 3,572 (+10405.88%)
Mutual labels:  state-management, state
Govern
Component-based state management for JavaScript.
Stars: ✭ 270 (+694.12%)
Mutual labels:  state-management, state
Redux Orm
A small, simple and immutable ORM to manage relational data in your Redux store.
Stars: ✭ 2,922 (+8494.12%)
Mutual labels:  state-management, state
Constate
React Context + State
Stars: ✭ 3,519 (+10250%)
Mutual labels:  hooks, state-management

use-global-context

Use-global-context is a new way to use useContext better.

build status npm version license

Features

  • Easy global state management with useState or useReducer.
  • Prevents the unnecessary renders.
  • useSelector function.
  • Support for SSR.

Why

The context API allows you to create a simple store in combination with hooks such as useState and useReducer.

However, it can lead to unnecessary renders if you don't split the context with proper granularity. It also doesn't have a feature like redux's useSelector. That means you have to memo. Please see the solutions.

On the other hand, while redux is appropriate for managing large amounts of state, I don't think it's necessary for small projects to adopt such a large library.

This library is intended to avoid the implementation costs of redux and also to prevent unnecessary renders, which is a problem with the context API.

Installation

You can install the package from npm.

npm install use-global-context

or

yarn add use-global-context

Usage

General

import React from "react";
import { createUseStateContext } from "use-global-context";

// You can add global state here. easy !!
const [useGlobalContext, ContextProvider] = createUseStateContext({
  counter: 0,
  message: "",
  app: {
    name: "use-global-context",
    description: "A easy global state management library",
  },
});

const App = () => (
  <ContextProvider>
    <Counter />
    <CounterButton />
  </ContextProvider>
)

const Counter = () => {
  // You can get the state value of the context as follows
  const counter = useGlobalContext(({ state }) => state.counter);

  return <p>counter: {counter}</p>;
};

const CounterButton = () => {
  const counterDispatch = useGlobalContext(({ dispatch }) => dispatch.counter);

  return (
    <>
      <button onClick={() => counterDispatch((c) => c + 1)}>+ 1</button>
      <button onClick={() => counterDispatch((c) => c - 1)}>- 1</button>
    </>
  );
};

API

createUseStateContext API

createUseStateContext is an API that executes each value of the argument as the value of useState and generates the result as hooks.

import React from "react";
import { createUseStateContext } from "use-global-context";

const [useGlobalContext, ContextProvider] = createUseStateContext({
  counter: 0,
  message: "",
  app: {
    name: "use-global-context",
    description: "A easy global state management library",
  }
});

You can use it as follows.

const state = useGlobalContext(({ state, dispatch }) => state);
// {
//   counter: 0,
//   message: '',
//   app: {
//     name: 'use-global-context',
//     description: 'A easy global state management library'
//   }
// }


const app = useGlobalContext(({ state, dispatch }) => state.app);
// {
//   name: "use-global-context",
//   description: "A easy global state management library",
// }

const dispatch = useGlobalContext(({ state, dispatch }) => dispatch)
// Each of the dispatch functions
// {
//   counter: ƒ dispatchAction,
//   message: ƒ dispatchAction,
//   app: ƒ dispatchAction
// }

const counterDispatch = useGlobalContext(({ state, dispatch }) => dispatch.counter);
// counter: ƒ dispatchAction,

createUseReducerContext API

createUseReducerContext is an API that executes each value of the argument as a value of useRecuer and generates the result as hooks.

Each argument of this API can be the same as useReducer.

import React from "react";
import { createUseReducerContext } from "use-global-context";

import { counterReducer, counterInitialState } from './reducer/counter'
import { messageReducer, messageInitialState } from './reducer/message'
import { userReducer, userInitialState } from './reducer/user'


export const [useGlobalContext, ContextProvider] = createUseReducerContext({
  counter: {
    reducer: counterReducer,
    initialState: counterInitialState,
  },
  message: {
    reducer: messageReducer,
    initialState: messageInitialState,
  },
  user: {
    reducer: userReducer,
    initialState: userInitialState,
  },
});

The usage is the same as createUseStateContext API.

TypeScript

Context value's type

You can define the value type of the context as follows. For example, to define the type of the value of a context created with createUseReducerContext, you can use the following.

import {
  createUseReducerContext,
  UseReducerContextValue,
} from 'use-global-context';
import { counterReducer, counterInitialState } from './reducer/counter'
import { messageReducer, messageInitialState } from './reducer/message'
import { userReducer, userInitialState } from './reducer/user'

const contextValue = {
  counter: {
    reducer: counterReducer,
    initialState: counterInitialState,
  },
  message: {
    reducer: messageReducer,
    initialState: messageInitialState,
  },
  user: {
    reducer: userReducer,
    initialState: userInitialState,
  },
}

const [useGlobalContext, ContextProvider] = createUseReducerContext(contextValue);

// You can define like this !!
type GlobalContextValue = UseReducerContextValue<typeof contextValue>;

const userNameSelector = (state: GlobalContextValue) => state.user.name

const userName = useGlobalContext(userNameSelector)

You can also define the type of the value of a context created by createUseStateContext by using the UseStateContextValue type as well.

Examples

CreateUseStateContext API example

This is an example of a counter app that uses the createUseStateContext API.

Notice that each time you increase/decrease the count, only the render of the Counter comport is running. (No unnecessary renders are happening.)


CreateUseReducerContext API example

Similar to the example above, this is an example of a counter app using the createUseReducerContext API.


License

MIT © kqito

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].