
Security News
/Company News
Socket Is Sponsoring Composer and Packagist
Socket has joined the new Composer and Packagist sponsorship program as a launch sponsor, supporting the team that keeps PHP's package ecosystem secure.
@huxy/use
Advanced tools
异步处理
const [result, updateResult] = useAsync({});
const update = useCallback(params => updateResult({res: fetchList({...commonParams, ...params})}, handleResult), []);
将图片 src 地址 改为 base64。
const Base64Image = ({src, ...rest}) => {
const url = useBase64(src);
return <img decoding="async" loading="lazy" {...rest} src={url} />;
};
可取消异步函数
const {cancelablePromise} = useCancelablePromise();
cancelablePromise(asyncFn, delay)
.then(res => {})
.catch(error => {});
点击区域外部时触发的事件
useClickAway(liRef, e => li.open && itemClick(e, li));
第一个参数为区域元素,第二个为点击区域外部时的回调函数。
useDebounce(fn, (delay = 60));
第一个参数为需要防抖的函数,第二个为防抖时间,默认 60ms。
延迟获取 state
const [delayOpen] = useDelayState(open, delay);
是否为第一次加载
const isFirst = useFirstMounted();
定时器
const [count, setCount] = useState(0);
const [stop, setStop] = useState(false);
const delay = 3000;
useInterval(
() => {
setCount(count + 1);
},
stop ? null : delay,
);
第一个参数为回调函数,第二个参数是延迟时间。当 delay 值为 null 或 false 时,会停止回调函数的执行。
上一个 state 值
const prevState = usePrevious(state);
requestAnimationFrame
const [state, setState] = useRaf({});
const state = useScroll(element);
传入监听滚动的元素,默认 window
返回 state 为滚动位置信息。
搜索函数
const [filterTree, setFilterTree] = useSearch(null, str2Dom);
setFilterTree(data, keyword, (fields = 'name'), (exact = false), (idKey = 'id'), (childKey = 'children'));
name,设为 null 则全局所有属性搜索状态管理
const [state, update, subscribe, clean] = useStore(name, initState);
input:
output:
可以自定义状态管理库:
const store = createStore();
const useStore = createContainer(store);
更新
const rerender = useUpdate();
rerender();
初次进入不执行,state 发生变更时执行。
useUpdateEffect(()=>{
...
return ()=>...;
},[state]);
const {width} = useWinResize();
// delay:节流时间
const state = useEleResize(ref, (delay = 250));
获取/设置 render 过程中的 ref。
const data = useCustomRef(0)();
FAQs
react hooks
The npm package @huxy/use receives a total of 222 weekly downloads. As such, @huxy/use popularity was classified as not popular.
We found that @huxy/use demonstrated a healthy version release cadence and project activity because the last version was released less than 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
/Company News
Socket has joined the new Composer and Packagist sponsorship program as a launch sponsor, supporting the team that keeps PHP's package ecosystem secure.

Research
/Security News
Benign-looking npm packages split malicious functionality across a dependency chain that deploys a cross-platform RAT targeting Alibaba developers.

Research
/Security News
Two Joyfill npm beta releases contain an import-time implant that uses blockchain transactions to retrieve a remote-access trojan.