state_use-state-with-dep.js

import { React } from '@gravityforms/libraries';

const { useState, useEffect } = React;

/**
 * @module useStateWithDep
 * @description Allows us to easily handle props as default state in hooks.
 *
 * @since 1.2.13
 *
 * @param {any} defaultValue The default value for the useState call.
 * @return {*} The state value and setter.
 *
 * @example
 * import { useStateWithDep } from '@gravityforms/react-utils';
 *
 * const Dialog = ( {
 * 	isOpen = false,
 * 	otherProps
 * } ) => {
 *      const [ dialogActive, setDialogActive ] = useStateWithDep( isOpen );
 * };
 *
 */
export default function useStateWithDep( defaultValue ) {
	const [ value, setValue ] = useState( defaultValue );

	useEffect( () => {
		setValue( defaultValue );
	}, [ defaultValue ] );
	return [ value, setValue ];
}