المشاريع
عرض على GitHub
the-road-to-learn-react/use-state-with-callback
حول
خطاف مخصّص يضيف دالة ردّ نداء إلى useState.
تطوير الويب والتطبيقاتالنمو المقاس للنجوم
- خلال 24 ساعة
- ٠
- خلال 7 أيام
- ٠
- خلال 15 يومًا
- ٠
- خلال شهر
- جارٍ جمع السجل
تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.
README.md
الأصلuseStateWithCallback React Hook
Custom hook to include a callback function for useState which was previously available for setState in class components. Read more about it here.
Installation
npm install use-state-with-callback
Usage
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;
Pitfalls
- When a state update is called with the current value and optimized away, the callback is never called.
useStateWithCallbackLazycalls the callback with the scope that existed before update, while this.setState callback can access the updated this.state andget something()computed values. This can't be fixed, but it's a problem for people who expect a drop-in replacement.- When
useStateWithCallbackLazystate is updated several times with batching (e.g. in an event handler), only the last update calls the callback.
Contribute
git clone [email protected]:the-road-to-learn-react/use-state-with-callback.gitcd use-state-with-callbacknpm installnpm run test
More
استكشف مشاريع مشابهة ومرتبطة
جارٍ التحميل…