UNPKG

@nutui/nutui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

51 lines (50 loc) 1.46 kB
import { useEffect, useRef } from "react"; import isequal from "lodash.isequal"; import { Events, getCurrentInstance } from "@tarojs/taro"; import { u as useForceUpdate } from "./use-force-update-CudFjCsy.js"; const customEvents = new Events(); function useCustomEventsPath(selector) { var _a; selector = selector || ""; const path = (_a = getCurrentInstance().router) === null || _a === void 0 ? void 0 : _a.path; return path ? `${path}__${selector}` : selector; } function useCustomEvent(selector, cb) { const path = useCustomEventsPath(selector); useEffect(() => { customEvents.on(path, cb); return () => { customEvents.off(path); }; }, []); const trigger = (args) => { customEvents.trigger(path, args); }; const off = () => { customEvents.off(path); }; return [trigger, off]; } function useParams(args) { const forceUpdate = useForceUpdate(); const stateRef = useRef(args); const currentRef = useRef(); const previousRef = useRef(); if (!isequal(currentRef.current, args)) { previousRef.current = currentRef.current; currentRef.current = args; stateRef.current = args; } const setParams = (args2) => { stateRef.current = Object.assign(Object.assign({}, stateRef.current), args2); forceUpdate(); }; const params = stateRef.current; return { params, setParams }; } export { useCustomEventsPath as a, useParams as b, customEvents as c, useCustomEvent as u };