Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@zyda/swr-internal-state
Advanced tools
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...
npm i @zyda/swr-internal-state
Or with yarn...
yarn 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...
const useUserInfo= () => useGlobalState<UserInfo>('user-info', { name: '', age: null });
// And in your components...
const UserName = () => {
const [userInfo, setUserInfo] = useUserInfo();
return <span>{userInfo.name}</span>;
}
const UpdateUser = () => {
const [userInfo, setUserInfo] = useUserInfo();
const updateUser = () => {
const userName = /* some logic */;
const userAge = /* some logic */;
setUserInfo({ name: userName, age: userAge });
}
// Component logic...
return (
<div>
{/* Some UI */}
<button onClick={updateUser}>Update User</button>
</div>
);
}
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).
useGlobalState<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.useLocalStorage<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
.[1.2.0]
remove
for local
storage.set
for global
and local
storage.Set
and Remove
functions types.FAQs
Use SWR to manage app's internal state
We found that @zyda/swr-internal-state demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.