
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-persistent-state
Advanced tools
Tools to simply persist component state in the local storage.
npm install --save react-persistant-state
or
npm install --save https://github.com/alexandreannic/react-persistent-state.git
Simply extend PersistentComponent from react-persistent-state instead of Component from react.
import React from 'react'
import {PersistentComponent} from 'react-persistent-state'
export class PersistentInput extends PersistentComponent {
state = {
value: '',
}
handleChange = event => {
this.setState({value: event.target.value})
}
render() {
return (
<div>
<input value={this.state.value} onChange={this.handleChange}/>
<button onClick={this.clearPersistentState}>Clear from local storage</button>
</div>
)
}
}
Use usePersistentState instead of useState.
It works the same except that it exposes another method to clear related local storage save.
import React from 'react'
import {usePersistentState} from 'react-persistent-state'
export function PersistentCounterHook() {
const [value, setValue, unpersist] = usePersistentState(0)
return (
<div>
<button onClick={() => setValue((prev) => prev + 1)}>{value}</button>
<button onClick={unpersist}>Clear from local storage</button>
</div>
)
}
When a component with persistant state is used multiples times inside a same component, react-persistent-state is not able to generate an unique key
to distinguish them from the local storage.
In this case you must provide an unique key as shown below:
function App() {
return (
<>
<Persistent persistentKey={1}/>
<Persistent persistentKey={2}/>
</>
)
}
export class Persistent extends PersistentComponent {
constructor(props) {
super(props, props.persistentKey)
}
...
}
export const Persistent = ({persistentKey}) => {
const [value, setValue, clearValue] = usePersistentState<string>(0, persistentKey)
...
}
FAQs
Abstract class to persist state in localstorage
We found that react-persistent-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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.