Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
use-state-with-callback
Advanced tools
[![Build Status](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-learn-react/u
Custom hook to include a callback function for useState which was previously available for setState in class components. Read more about it here.
npm install use-state-with-callback
useStateWithCallback:
import * as React from 'react';
import useStateWithCallback from 'use-state-with-callback';
// Note: cannot be used on the server-side (e.g. Next.js)
// import { useStateWithCallbackInstant } from 'use-state-with-callback';
const App = () => {
const [count, setCount] = useStateWithCallback(0, currentCount => {
console.log('render, then callback.');
console.log('otherwise use useStateWithCallbackInstant()');
if (currentCount > 1) {
console.log('Threshold of over 1 reached.');
} else {
console.log('No threshold reached.');
}
});
// const [count, setCount] = useStateWithCallbackInstant(0, currentCount => {
// console.log('render with instant callback.');
// if (currentCount > 1) {
// console.log('Threshold of over 1 reached.');
// } else {
// console.log('No threshold reached.');
// }
// });
const handleClick = () => {
setCount(count + 1);
};
return (
<div>
{count}
<button type="button" onClick={handleClick}>
Increase
</button>
</div>
);
};
export default App;
useStateWithCallbackLazy:
import * as React from 'react';
import { useStateWithCallbackLazy } from 'use-state-with-callback';
const App = () => {
const [count, setCount] = useStateWithCallbackLazy(0);
const handleClick = () => {
setCount(count + 1, (currentCount) => {
if (currentCount > 1) {
console.log('Threshold of over 1 reached.');
} else {
console.log('No threshold reached.');
}
});
};
return (
<div>
<p>{count}</p>
<button type="button" onClick={handleClick}>
Increase
</button>
</div>
);
};
export default App;
useStateWithCallbackLazy
calls the callback with the scope that existed before update, while this.setState callback can access the updated this.state and get something()
computed values. This can't be fixed, but it's a problem for people who expect a drop-in replacement.useStateWithCallbackLazy
state is updated several times with batching (e.g. in an event handler), only the last update calls the callback.git clone git@github.com:the-road-to-learn-react/use-state-with-callback.git
cd use-state-with-callback
npm install
npm run test
FAQs
[![Build Status](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback.svg?branch=master)](https://travis-ci.org/the-road-to-learn-react/use-state-with-callback) [![Greenkeeper badge](https://badges.greenkeeper.io/the-road-to-learn-react/u
The npm package use-state-with-callback receives a total of 5,686 weekly downloads. As such, use-state-with-callback popularity was classified as popular.
We found that use-state-with-callback 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.