From 8ef4c88c6b67a6cd96f7e895f220c20739ef8811 Mon Sep 17 00:00:00 2001 From: Velenir Date: Tue, 3 Mar 2020 12:39:59 +0300 Subject: [PATCH] memoize toggle function --- src/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 6dba248..c8b1262 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect, useRef } from 'react'; +import React, { useState, useEffect, useRef, useCallback } from 'react'; import ReactDOM from 'react-dom'; import PropTypes from 'prop-types'; import classNames from 'classnames'; @@ -140,14 +140,14 @@ export const useModali = (options) => { isModalVisibleRef.current = isModalVisible; let timeoutHack; - function toggle() { + const toggle = useCallback(() => { timeoutHack = setTimeout(() => { setIsModalVisible(!isModalVisibleRef.current); clearTimeout(timeoutHack); }, 10); - setIsShown(!isShown); + setIsShown(isShown => !isShown); setHasToggledBefore(true); - } + }, []) function handleKeyDown(event) { if (event.keyCode !== 27 || (options && options.keyboardClose === false)) return;