moment-hooks
v2.1.0
Published
Incredibly awesome hooks
Downloads
370
Maintainers
Readme
moment-hooks
A module to group all the best hooks togheter.
Prerequisites
- React 16.8 or greater
Install
yarn install moment-hooks
List of hooks
- useOutsideClick
- useLockBodyScroll
- useArray
- useDebounce
- useWindowSize
- useQueryStringState
- useWhyDidYouUpdate
useOutsideClick
calls a function when a click occurs on the outside of the ref element
Parameters
- 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"));
useArray
makes handling of state array easier
Parameters
- The initial value of the array
import { useArray } from 'moment-hooks';
const [list, actions] = useArray(["element 1"]);
Return
the hook returns an array with two elements
- The array stored in state
- The actions you can do on the array
Actions
- 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
useLockBodyScroll
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>;
}
useDebounce
Reduce the amount of time is updated. Useful when dealing with fetch.
Parameters
- 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);
useWindowSize
returns the size of the window
import { useWindowSize } from 'moment-hooks';
const { width, height } = useWindowSize();
useSize
returns the size of the reference
import { useSize } from 'moment-hooks';
const { width, height } = useSize();
Parameters
- ref: a React ref
useQueryStringState
Works the same as useState just that it stores it's state in the querystring of the url.
Parameters
- defaultState: Exactly the same as the default state of useState
- Object with 2 keys:
fromString
A way to serialize the url string to a value in the state
toString
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(),
},
}
);
useWhyDidYouUpdate
logs the reason why a component has been updated
Parameters
- The name of the component
- The props
import { useWhyDidYouUpdate } from 'moment-hooks';
useWhyDidYouUpdate('Register', props);