UNPKG

@fluent-windows/hooks

Version:
39 lines (36 loc) 787 B
/** * EventBus hooks * * Demo * import { useAction, useDispatch } from '@fluent-windows/hooks' * * // parent * useAction( * 'navigation/active', * (payload) => { * // ... * }, * [] * ) * * // children * const dispatch = useDispatch({ type: 'navigation/active', payload: 'xxx' }) * function handleClick() { * dispatch() * } * <button onClick={handleClick}>child</button> */ import * as React from 'react'; import { subscribe, unsubscribe, dispatch } from './subscribers'; function useAction(type, callback, deps = []) { React.useEffect(() => { subscribe(type, callback); return () => { unsubscribe(type, callback); }; }, deps); // eslint-disable-line return dispatch.bind(undefined, { type }); } export default useAction;