A module to group all the best hooks togheter.
- React 16.8 or greater
yarn install moment-hooks
- useOutsideClick
- useLockBodyScroll
- useArray
- useDebounce
- useWindowSize
- useQueryStringState
- useWhyDidYouUpdate
calls a function when a click occurs on the outside of the ref element
- The ref of the element
- The function that is called when a click occurs outside the ref
import { useOutsideClick } from 'moment-hooks';
const ref = React.useRef();
useOutsideClick(ref, () => console.log("On click outside"));
makes handling of state array easier
- The initial value of the array
import { useArray } from 'moment-hooks';
const [list, actions] = useArray(["element 1"]);
the hook returns an array with two elements
- The array stored in state
- The actions you can do on the array
- push - Adds the first parameter of the funtion to the end of the array
- removeIndex - Removes the element at the index provided as the first parameter of the function
Disables the scroll on body. Widely used in modals.
###Parameters
- If it should be hidden. Defaults to true.
import { useLockBodyScroll } from 'moment-hooks';
useLockBodyScroll();
or if you have it in a modal
import { useLockBodyScroll } from 'moment-hooks';
const Modal = ({ isOpen }) => {
useLockBodyScroll(isOpen);
return <div>Modal</div>;
}
Reduce the amount of time is updated. Useful when dealing with fetch.
- The value to debounce
- The debounce delay in milliseconds. This is the time where no update must ocure to inputValue in order for the debounced value to be updated
import { useDebounce } from 'moment-hooks';
const [inputValue, setInputValue] = React.useState("A value");
const debouncedInputValue = useDebounce(inputValue, 100);
returns the size of the window
import { useWindowSize } from 'moment-hooks';
const { width, height } = useWindowSize();
returns the size of the reference
import { useSize } from 'moment-hooks';
const { width, height } = useSize();
- ref: a React ref
Works the same as useState just that it stores it's state in the querystring of the url.
- defaultState: Exactly the same as the default state of useState
- Object with 2 keys:
A way to serialize the url string to a value in the state
A way to stringify the state.
const [reportSettings, setReportSettings] = useQueryStringState(
{
from: moment()
.subtract(1, 'months')
.toDate(),
to: moment().toDate(),
},
{
fromString: {
from: from => moment(from).toDate(),
to: to => moment(to).toDate(),
},
toString: {
from: from => moment(from).format(),
to: to => moment(to).format(),
},
}
);
logs the reason why a component has been updated
- The name of the component
- The props
import { useWhyDidYouUpdate } from 'moment-hooks';
useWhyDidYouUpdate('Register', props);