Files
react/shells/dev/app/InspectableElements/CustomHooks.js
Brian Vaughn 33deb79ce4 Added simple hooks support (same as in legacy DevTools for now)
I had to add a couple of  comments because Flow was being a mysterious little shit and I got tired of trying to work around it.
2019-02-05 09:23:14 +00:00

80 lines
1.9 KiB
JavaScript

// @flow
import React, {
forwardRef,
Fragment,
memo,
useCallback,
// $FlowFixMe Flow doesn't yet know about this hook
useDebugValue,
useEffect,
useState,
} from 'react';
function useNestedInnerHook() {
return useState(123);
}
function useNestedOuterHook() {
return useNestedInnerHook();
}
function FunctionWithHooks(props: any, ref: React$Ref<any>) {
const [count, updateCount] = useState(0);
// Custom hook with a custom debug label
const debouncedCount = useDebounce(count, 1000);
const onClick = useCallback(
function onClick() {
updateCount(count + 1);
},
[count]
);
// Tests nested custom hooks
useNestedOuterHook();
return <button onClick={onClick}>Count: {debouncedCount}</button>;
}
const MemoWithHooks = memo(FunctionWithHooks);
const ForwardRefWithHooks = forwardRef(FunctionWithHooks);
export default function CustomHooks() {
return (
<Fragment>
<FunctionWithHooks />
<MemoWithHooks />
<ForwardRefWithHooks />
</Fragment>
);
}
// Below copied from https://usehooks.com/
function useDebounce(value, delay) {
// State and setters for debounced value
const [debouncedValue, setDebouncedValue] = useState(value);
// Show the value in DevTools
useDebugValue(debouncedValue);
useEffect(
() => {
// Update debounced value after delay
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
// Cancel the timeout if value changes (also on delay change or unmount)
// This is how we prevent debounced value from updating if value is changed ...
// .. within the delay period. Timeout gets cleared and restarted.
return () => {
clearTimeout(handler);
};
},
[value, delay] // Only re-call effect if value or delay changes
);
return debouncedValue;
}
// Above copied from https://usehooks.com/