Skip to content

Instantly share code, notes, and snippets.

@drublic
Last active September 21, 2019 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drublic/cf1e4ad38693f99a803d931b332b26e9 to your computer and use it in GitHub Desktop.
Save drublic/cf1e4ad38693f99a803d931b332b26e9 to your computer and use it in GitHub Desktop.
Without Conceptual Hooks
import React, { useState, useEffect, useCallback } from 'react';
import Loader from '../Loader';
const Button = ({ label, isLoading }) => {
const [hasFocus, setHasFocus] = useState<boolean>(false);
const [hasLoader, setHasLoader] = useState<boolean>(false);
const handleFocus = useCallback(() => {
setHasFocus(true);
}, []);
const handleBlur = useCallback(() => {
setHasFocus(false);
}, []);
useEffect(() => {
if (isLoading && !hasFocus) {
return setHasLoader(true);
}
return setHasLoader(false);
}, [isLoading, hasFocus]);
return (
<button type="button" onFocus={handleFocus} onBlur={handleBlur}>
{hasLoader ? <Loader /> : label}
</button>
);
};
export default Button;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment