@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
52 lines (51 loc) • 1.69 kB
JavaScript
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { useEffect, useRef } from "react";
import isEqual from "react-fast-compare";
import { Events, getCurrentInstance } from "@tarojs/taro";
import { useForceUpdate } from "./use-force-update";
export var customEvents = new Events();
export function useCustomEventsPath(selector) {
var _getCurrentInstance_router;
selector = selector || '';
var path = (_getCurrentInstance_router = getCurrentInstance().router) === null || _getCurrentInstance_router === void 0 ? void 0 : _getCurrentInstance_router.path;
return path ? "".concat(path, "__").concat(selector) : selector;
}
export function useCustomEvent(selector, cb) {
var path = useCustomEventsPath(selector);
useEffect(function() {
customEvents.on(path, cb);
return function() {
customEvents.off(path);
};
}, []);
var trigger = function(args) {
customEvents.trigger(path, args);
};
var off = function() {
customEvents.off(path);
};
return [
trigger,
off
];
}
export function useParams(args) {
var forceUpdate = useForceUpdate();
var stateRef = useRef(args);
var currentRef = useRef();
var previousRef = useRef();
if (!isEqual(currentRef.current, args)) {
previousRef.current = currentRef.current;
currentRef.current = args;
stateRef.current = args;
}
var setParams = function(args) {
stateRef.current = _object_spread({}, stateRef.current, args);
forceUpdate();
};
var params = stateRef.current;
return {
params: params,
setParams: setParams
};
}