f
Home

Published

- menos de un minuto leyendo

Dos Hooks de tiempo y Session Storage

img of Dos Hooks de tiempo y Session Storage

useSessionStorage

   import { useState, useEffect } from "react";

function getSessionStorageOrDefault(key, defaultValue) {
	const stored = sessionStorage.getItem(key);
	if (!stored) {
		return defaultValue;
	}
	return JSON.parse(stored);
}

export default function useSessionStorage(key, defaultValue) {
	const [value, setValue] = useState(getSessionStorageOrDefault(key, defaultValue));

	useEffect(() => {
		sessionStorage.setItem(key, JSON.stringify(value));
	}, [key, value]);

	return [value, setValue];
}

useDaysDifference

   export default function (date\_1: Date, date\_2: Date) {\
 const difference = date\_1.getTime() - date\_2.getTime();\
 const TotalDays = Math.ceil(difference / (1000 \* 3600 \* 24));\
 return TotalDays;\
}

Implementado originalmente por Bhavin Shah en Stack Overflow