
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
create-react-state
Advanced tools
React state management you already know how to use
useState
import React from 'react';
import { createState } from 'create-react-state';
// Create the state with an initial value
const counterState = createState(0)
function Counter() {
// Use this hook in any component that needs to react to the state
const [count, setCount] = counterState.useState();
return (
<div>
<p>{count}</p>
<button onClick={() => setCount(count + 1)}>+</button>
</div>
);
}
Always returns the current state value. Can be used inside or outside of react components.
console.log(counterState.value)
Mutate the state inside or outside of react components
counterState.set(counterState.value + 1)
Subscribe to state inside or outside of react components
counterState.subscribe(x => {
console.log('counterState changed', x)
})
Wait until state is ready with a promise.
// Resolves when count goes above 5.
// If the count is already above 5 it will resolve immediately.
const highCount = await counterState.waitFor(count => {
return count > 5 ? count : false
})
Note the promise will resolve if anything other than false
, undefined
or null
is returned.
const [count, setCount] = counterState.useState()
// Component only re-renders if the value of the count is even
const [evenCount, setCount] = counterState.useState(x => x % 2 == 0)
By default the selector function is cached. In some rarer cases the function might be dynamic. For example:
const users = createState({
alice: { name: 'Alice', age: 20 },
bob: { name: 'Bob', age: 30 },
chad: { name: 'Chad', age: 40 },
});
function Foo() {
const [name, setName] = useState('alice')
// Add name as a dependency to the selector
const [user] = users.useState(x => x[name], [name]);
return (
<div>
<p>{user.name} {user.age}</p>
<button onClick={() => setName('alice')}>Alice</button>
<button onClick={() => setName('bob')}>Bob</button>
<button onClick={() => setName('chad')}>Chad</button>
</div>
);
}
FAQs
React Create State is a global useState generator.
The npm package create-react-state receives a total of 0 weekly downloads. As such, create-react-state popularity was classified as not popular.
We found that create-react-state demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.