
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
@gk3000/use-previous
Advanced tools
A lightweight and simple custom React hook that helps you track the previous value of a state or prop in your functional components.
useRef
to persist values across renders without triggering re-renders.Install the hook via npm:
npm install @gk3000/use-previous
or via yarn:
yarn add @gk3000/use-previous
##U sage
This hook is perfect for tracking the previous value of a piece of state or a prop in your component. Here's an example of how to use it in a functional component:
import React, { useState } from 'react';
import usePrevious from '@gk3000/use-previous';
function Counter() {
const [count, setCount] = useState(0);
// Track the previous value of the count state
const previousCount = usePrevious(count);
return (
<div>
<h1>Current Count: {count}</h1>
<h2>Previous Count: {previousCount}</h2>
<button onClick={() => setCount(count + 1)}>Increment</button>
<button onClick={() => setCount(count - 1)}>Decrement</button>
</div>
);
}
export default Counter;
const previousValue = usePrevious(value);
value
(any
): The state or prop value you want to track.
previousValue
: The previous value of the state or prop before the last update. On the initial render, it will return undefined
.This hook can also track previous values of more complex data types, such as objects or arrays:
import React, { useState } from 'react';
import usePrevious from '@gk3000/use-previous';
function UserInfo() {
const [user, setUser] = useState({ name: 'John', age: 30 });
const previousUser = usePrevious(user);
return (
<div>
<h1>Current User: {user.name}, Age: {user.age}</h1>
<h2>Previous User: {previousUser?.name}, Age: {previousUser?.age}</h2>
<button onClick={() => setUser({ name: 'Jane', age: 25 })}>
Change User
</button>
</div>
);
}
export default UserInfo;
This project is licensed under the MIT License.
FAQs
React hook to get the previous value of a prop or state
We found that @gk3000/use-previous demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.