Created
March 26, 2019 12:36
-
-
Save nikolay-borzov/8d352ce5468548a211e6c7f731b64751 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Button = forwardRef<CoralButton, ButtonProps>((props, ref) => { | |
const { children, onClick, title, ...coralButtonProps } = props; | |
const buttonRef = useRef<CoralButton>(null); | |
useLayoutEffect(() => { | |
if (ref) { | |
if (isFunction(ref)) { | |
ref(buttonRef.current); | |
} else { | |
(ref as any).current = buttonRef.current; | |
} | |
} | |
}, [buttonRef]); | |
useEffect(() => { | |
const button = buttonRef.current!; | |
if (onClick) { | |
button.addEventListener('click', onClick); | |
return () => { | |
button.removeEventListener('click', onClick); | |
}; | |
} | |
}, [onClick]); | |
return ( | |
<coral-button | |
ref={buttonRef} | |
title={title} | |
{...getCustomElementAttributes(coralButtonProps)}> | |
{children} | |
</coral-button> | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just use
useImperativeHandle
https://ru.reactjs.org/docs/hooks-reference.html#useimperativehandlecode sample, see
ChildInput.tsx
component