
Security News
NIST Under Federal Audit for NVD Processing Backlog and Delays
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
react-cognito-auth
Advanced tools
import { quantumState } from 'react-quantum-state'
The quantumState method takes 3 properties:
interface quantumStateProps {
id: string,
initialValue?: any,
returnValue?: boolean
}
// a component that initializes, updates and displays the global state
const StateManager = () => {
const [count, setCount] = quantumState({ id: "counter", initialValue: 0 })
return (
<div>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<button onClick={() => setCount(count - 1)}>
Decrement
</button>
<p>
Current count is: {count}
</p>
</div>
)
}
// a component that only sets state and doesnt rerender on updated state
const StateSetter = () => {
const [_, setCount] = quantumState({ id: "counter", returnValue: false })
return (
<div>
<button onClick={() => setCount(count + 1)}>
Increment
</button>
<button onClick={() => setCount(count - 1)}>
Decrement
</button>
</div>
)
}
// a component that only displays the global state
const StateObserver = () => {
const [count] = quantumState({ id: "counter" })
return (
<div>
<p>
Current count is: {count}
</p>
</div>
)
}
import { setQuantumValue } from 'react-quantum-state'
const resetCounter = () => setQuantumValue("counter", 0)
// this component sets the global state for "counter" to 0
const StateResetter = () => (
<div>
<button onClick={() => resetCounter()}>
Reset
</button>
</div>
)
A simple usage within a functional component will look like this:
import { quantumReducer } from 'react-quantum-state'
// at this point we assume that our reducer contains a state object with { firstName, lastName, age, edits }
// everytime we edit the properties of the object, the edits counter will be incremented
const ComplexStateManager = () => {
const { state, dispatch } = quantumReducer({ id: "infos" })
const {
firstName,
lastName,
age,
edits
} = state
return (
<div>
<h4>
You have edited the data: {edits} times
</h4>
<input
type="text"
value={firstName}
onChange={({ target: { value } }) =>
dispatch({ type: "changeAttribute", payload: { attribute: "firstName", value }})
}/>
<input
type="text"
value={lastName}
onChange={({ target: { value } }) =>
dispatch({ type: "changeAttribute", payload: { attribute: "lastName", value }})
}/>
<input
type="number"
value={age}
onChange={({ target: { value } }) =>
dispatch({ type: "changeAttribute", payload: { attribute: "age", value }})
}/>
</div>
)
}
At this point we still need to define out reducer and set up our global store that manages that reducer.
For this example the reducer will look like this:
const reducer = (
state,
action
) => {
switch (action.type) {
case "changeAttribute":
return {
...state,
[action.data.attribute]: action.data.value,
edits: state.edits + 1
}
default:
return state
}
};
Before we can access a stores state, we need to initialize it.
import { initializeStores } from 'react-quantum-state'
The initializeStores method takes an array with store configurations. You can initialize as many stores as you want to. Each store will be treated as a single state object. In contrast to Redux, a dispatched action will only be emitted to that stores reducer and will not effect any other stores.
To store has the following properties
interface StoreProperties {
id: string,
reducer: Function,
initialState: any
}
Taking the above defined reducer, we can initiate a store like this:
initializeStores([
{
id: "infos",
reducer, // as defined above
initialState: {
firstName: "",
lastName: "",
age: 0,
edits
}
}
])
For flexible state management we can also interact with our stores from outside a components
import { dispatchToStore } from 'react-quantum-state'
const resetEdits = () => dispatchToStore({ id: "infos", action: { type: "changeAttribute": { data: { attribute: "edits", value: 0 } } } })
// call from outside component
resetEdits()
// or call from within component
const ResetEdits = () => {
return (
<div>
<button onClick={() => resetEdits()}>
Reset Edits Count
</button>
</div>
)
}
FAQs
An aws cognito library for authentication
The npm package react-cognito-auth receives a total of 0 weekly downloads. As such, react-cognito-auth popularity was classified as not popular.
We found that react-cognito-auth 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
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.
Security News
TypeScript Native Previews offers a 10x faster Go-based compiler, now available on npm for public testing with early editor and language support.