New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

use-global-state-react

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-global-state-react

Simple useGlobalState hook for react

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

useGlobalState

Simple useGlobalState hook for React!

Do you need your data shared across other components, and you simply don't want to pass props all the way down, create context or use state management tool?

Just use useGlobalState, it's really simple and does all the magic for you:

  • first argument is a key for your store
  • second argument is optional - initial value

Try Codesandbox example

Example:

import { useGlobalState } from 'use-global-state-react';

const TASK_STORE_KEY = 'tasks';

const Tasks = () => {
    const [tasks, setTasks] = useGlobalState<string[]>(TASK_STORE_KEY, []);
    
    ...
}

and then you can use the same hook everywhere, data will be shared across components and component will rerender if changes happened in store:

const AddTaskButton = () => {
    const [, setTasks] = useGlobalState(TASK_STORE_KEY, []);
    
    const onTaskAdd = (newTask: stirng) => setTasks(prev => [...prev, newTask]);
    
    ...
}

const TasksTotal = () => {
    const [tasks] = useGlobalState<string[]>(TASK_STORE_KEY, []);
    
    return tasks.length;
}

or use helper createGlobalStore to create custom hook with shared data:

import { createGlobalStore } from 'use-global-state-react';

const useTasks = createGlobalStore<string[]>(TASK_STORE_KEY, []);


const Tasks = () => {
    const [tasks, setTasks] = useTasks();
    ...
}

Installation

  npm i use-global-state-react

or

  yarn add use-global-state-react

Keywords

state

FAQs

Package last updated on 27 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts