From a5a5c5205875ad154d534bc1761df88a6e23576f Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Wed, 22 Jun 2022 09:32:57 -0300 Subject: [PATCH] Chore: Migrate containers/Toast to hooks (#4309) --- app/containers/Toast.tsx | 76 +++++++++++++++------------------------- 1 file changed, 29 insertions(+), 47 deletions(-) diff --git a/app/containers/Toast.tsx b/app/containers/Toast.tsx index c7f0f1045..0834ba3f6 100644 --- a/app/containers/Toast.tsx +++ b/app/containers/Toast.tsx @@ -1,11 +1,10 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { StyleSheet } from 'react-native'; import EasyToast from 'react-native-easy-toast'; -import { themes } from '../lib/constants'; -import sharedStyles from '../views/Styles'; import EventEmitter from '../lib/methods/helpers/events'; -import { TSupportedThemes, withTheme } from '../theme'; +import { useTheme } from '../theme'; +import sharedStyles from '../views/Styles'; const styles = StyleSheet.create({ toast: { @@ -21,54 +20,37 @@ const styles = StyleSheet.create({ export const LISTENER = 'Toast'; -interface IToastProps { - theme?: TSupportedThemes; -} +let listener: Function; +let toast: EasyToast | null | undefined; -class Toast extends React.Component { - private listener?: Function; +const Toast = (): React.ReactElement => { + const { colors } = useTheme(); - private toast: EasyToast | null | undefined; + useEffect(() => { + listener = EventEmitter.addEventListener(LISTENER, showToast); + return () => { + EventEmitter.removeListener(LISTENER, listener); + }; + }, []); - componentDidMount() { - this.listener = EventEmitter.addEventListener(LISTENER, this.showToast); - } + const getToastRef = (newToast: EasyToast | null) => (toast = newToast); - shouldComponentUpdate(nextProps: any) { - const { theme } = this.props; - if (nextProps.theme !== theme) { - return true; - } - return false; - } - - componentWillUnmount() { - if (this.listener) { - EventEmitter.removeListener(LISTENER, this.listener); - } - } - - getToastRef = (toast: EasyToast | null) => (this.toast = toast); - - showToast = ({ message }: { message: string }) => { - if (this.toast && this.toast.show) { - this.toast.show(message, 1000); + const showToast = ({ message }: { message: string }) => { + if (toast && toast.show) { + toast.show(message, 1000); } }; - render() { - const { theme } = this.props; - return ( - - ); - } -} + return ( + + ); +}; -export default withTheme(Toast); +export default Toast;