Gathering detailed insights and metrics for @zyda/swr-internal-state
Gathering detailed insights and metrics for @zyda/swr-internal-state
npm install @zyda/swr-internal-state
Typescript
Module System
Node Version
NPM Version
69.5
Supply Chain
94.8
Quality
80.2
Maintenance
100
Vulnerability
100
License
TypeScript (98.64%)
JavaScript (1.36%)
Total Downloads
46,033
Last Day
15
Last Week
65
Last Month
362
Last Year
7,526
35 Stars
28 Commits
2 Forks
15 Watching
1 Branches
14 Contributors
Latest Version
1.2.0
Package Id
@zyda/swr-internal-state@1.2.0
Unpacked Size
23.17 kB
Size
6.92 kB
File Count
28
NPM Version
6.14.17
Node Version
14.20.1
Cumulative downloads
Total Downloads
Last day
400%
15
Compared to previous day
Last week
-15.6%
65
Compared to previous week
Last month
4%
362
Compared to previous month
Last year
-44.3%
7,526
Compared to previous year
1
4
This package provides 2 hooks that can be used to manage react (or Next.js) project's internal state in ways: in memory state, and local storage persistent state.
This package is based on swr as they have built a very good caching method that can be generalized and used with different use cases other than fetching and caching data from an external service. Like managing internal app state and syncing that state between different components.
The 2 hooks that we expose are:
useGlobalState
: used for managing and syncing in-memory state.useLocalStorage
: used for managing and syncing local storage persistent state.This package was made with the intention to be used to manage the internal states of projects that rely heavily on external services and has simple internal states. So -for small/simple states- it can be a convenient replacement for Redux
, PullState
, and even React's Context
API.
With npm...
1npm i @zyda/swr-internal-state
Or with yarn...
1yarn add @zyda/swr-internal-state
The 2 used hooks accept similar parameters and have similar return types. Each of them accept a key and a default value. And both of them return an array of the state and its management functions (the exact interface of the hooks is explained below).
These 2 hooks are intended to be used as a building block for other custom hooks that are used to encapsulate the whole state.
I.e. If you want to save a state that contains user info, you should create a custom hook useUserInfo
that looks like the following...
1const useUserInfo= () => useGlobalState<UserInfo>('user-info', { name: '', age: null }); 2 3// And in your components... 4const UserName = () => { 5 const [userInfo, setUserInfo] = useUserInfo(); 6 return <span>{userInfo.name}</span>; 7} 8 9const UpdateUser = () => { 10 const [userInfo, setUserInfo] = useUserInfo(); 11 12 const updateUser = () => { 13 const userName = /* some logic */; 14 const userAge = /* some logic */; 15 setUserInfo({ name: userName, age: userAge }); 16 } 17 18 // Component logic... 19 20 return ( 21 <div> 22 {/* Some UI */} 23 <button onClick={updateUser}>Update User</button> 24 </div> 25 ); 26} 27
When you use setUserInfo
it updates userInfo
state in all the components that use it as if it was raised to their parent and shared from it (but that's not how SWR manage their state).
1useGlobalState<T>(key: string, defaultValue: T): [T, setValue: (T) => void]
This hook is used to manage and sync in-memory state between different components. The saved state is not persisted and will be reset with page refresh.
Parameters:
key
(required): a unique key to the state that you want to manage. Used to differentiate states from each other (for example: to tell the difference between user-info
and user-cart
states)defaultValue
(optional, defaults to null
): The initial value of that state.Return values: The hook returns an array that contains 2 values:
defaultValue
.setState
function: A function that accepts 1 parameter: the new state.1useLocalStorage<T>(key: string, defaultValue: T): [T, setValue: (T) => void, removeValue: () => void]
This hook is used to manage and sync persisted state between different components. The saved state will be saved to the local storage and it will survive refreshing the page and closing the browser.
Parameters:
key
(required): a unique key to the state that you want to manage. Used to differentiate states from each other (for example: to tell the difference between user-info
and user-cart
states)defaultValue
(optional, defaults to value stored in locale storage if exisits or null
): The initial value of that state.Return values:
defaultValue
.setState
function: A function that accepts 1 parameter: the new state.removeValue
function: A function that does not accept any value. Used to clear the local storage from the saved state and sets the state to defaultValue
.No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
Found 5/13 approved changesets -- score normalized to 3
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
21 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More