1- import React , { Component } from 'react' ;
1+ import React , { Component , useEffect , useState } from 'react' ;
22import { View , Text , StyleSheet , Animated , StyleProp , ViewStyle } from 'react-native' ;
33import Icon , { IconsName } from '../Icon' ;
44
@@ -13,29 +13,22 @@ export interface ToastProps {
1313 style ?: StyleProp < ViewStyle > ;
1414}
1515
16- export default class ToastContainer extends Component < ToastProps , any > {
17- static defaultProps = {
18- duration : 3 ,
19- type : 'info' ,
20- } ;
16+ export default function ToastContainer ( props : ToastProps ) {
17+ const [ anim , setAnim ] = useState < Animated . CompositeAnimation | null | undefined > ( ) ;
2118
22- anim : Animated . CompositeAnimation | null | undefined ;
19+ const [ state , setState ] = useState ( {
20+ fadeAnim : new Animated . Value ( 1 ) ,
21+ } ) ;
2322
24- constructor ( props : ToastProps ) {
25- super ( props ) ;
26- this . state = {
27- fadeAnim : new Animated . Value ( 1 ) ,
28- } ;
29- }
30- componentDidMount ( ) {
31- const { onClose, onAnimationEnd } = this . props ;
32- const duration = this . props . duration as number ;
23+ useEffect ( ( ) => {
24+ const { onClose, onAnimationEnd } = props ;
25+ const duration = props . duration as number ;
3326 const timing = Animated . timing ;
34- if ( this . anim ) {
35- this . anim = null ;
27+ if ( anim ) {
28+ setAnim ( null ) ;
3629 }
3730 const animArr = [
38- timing ( this . state . fadeAnim , {
31+ timing ( state . fadeAnim , {
3932 toValue : 1 ,
4033 duration : 200 ,
4134 useNativeDriver : true ,
@@ -44,17 +37,17 @@ export default class ToastContainer extends Component<ToastProps, any> {
4437 ] ;
4538 if ( duration > 0 ) {
4639 animArr . push (
47- timing ( this . state . fadeAnim , {
40+ timing ( state . fadeAnim , {
4841 toValue : 0 ,
4942 duration : 200 ,
5043 useNativeDriver : true ,
5144 } ) ,
5245 ) ;
5346 }
54- this . anim = Animated . sequence ( animArr ) ;
55- this . anim . start ( ( ) => {
47+ setAnim ( Animated . sequence ( animArr ) ) ;
48+ anim ? .start ( ( ) => {
5649 if ( duration > 0 ) {
57- this . anim = null ;
50+ setAnim ( null ) ;
5851 if ( onClose ) {
5952 onClose ( ) ;
6053 }
@@ -63,18 +56,18 @@ export default class ToastContainer extends Component<ToastProps, any> {
6356 }
6457 }
6558 } ) ;
66- }
6759
68- componentWillUnmount ( ) {
69- if ( this . anim ) {
70- this . anim . stop ( ) ;
71- this . anim = null ;
72- }
73- }
60+ return ( ) => {
61+ if ( anim ) {
62+ anim . stop ( ) ;
63+ setAnim ( null ) ;
64+ }
65+ } ;
66+ } , [ ] ) ;
7467
75- renderIcon = ( ) => {
76- const { type } = this . props ;
77- let icon = this . props . icon ;
68+ const renderIcon = ( ) => {
69+ const { type } = props ;
70+ let icon = props . icon ;
7871 let color = '' ;
7972 if ( ! icon ) {
8073 switch ( type ) {
@@ -101,24 +94,27 @@ export default class ToastContainer extends Component<ToastProps, any> {
10194 return { icon, color } ;
10295 } ;
10396
104- render ( ) {
105- const { content, type, style } = this . props ;
97+ const { content, type, style } = props ;
10698
107- return (
108- < View style = { [ styles . container , style ] } >
109- < View style = { [ styles . innerContainer ] } >
110- < Animated . View style = { { opacity : this . state . fadeAnim } } >
111- < View style = { [ styles . content , styles [ type ] ] } >
112- < Icon name = { this . renderIcon ( ) . icon } size = { 16 } style = { [ styles . icon ] } color = { this . renderIcon ( ) . color } />
113- < Text > { content } </ Text >
114- </ View >
115- </ Animated . View >
116- </ View >
99+ return (
100+ < View style = { [ styles . container , style ] } >
101+ < View style = { [ styles . innerContainer ] } >
102+ < Animated . View style = { { opacity : state . fadeAnim } } >
103+ < View style = { [ styles . content , styles [ type ] ] } >
104+ < Icon name = { renderIcon ( ) . icon } size = { 16 } style = { [ styles . icon ] } color = { renderIcon ( ) . color } />
105+ < Text > { content } </ Text >
106+ </ View >
107+ </ Animated . View >
117108 </ View >
118- ) ;
119- }
109+ </ View >
110+ ) ;
120111}
121112
113+ ToastContainer . defaultProps = {
114+ duration : 3 ,
115+ type : 'info' ,
116+ } as ToastProps ;
117+
122118const styles = StyleSheet . create ( {
123119 container : {
124120 flex : 1 ,
0 commit comments