1- import React , { Component } from 'react'
1+ import React , { useCallback , useMemo , useState } from 'react'
22import { StyleSheet , View } from 'react-native'
3- import withCacheBust from './withCacheBust'
43import SectionFlex from './SectionFlex'
54import FastImage , { FastImageProps } from 'react-native-fast-image'
65import Section from './Section'
76import FeatureText from './FeatureText'
7+ import { useCacheBust } from './useCacheBust'
88
99const GIF_URL =
1010 'https://cdn-images-1.medium.com/max/1600/1*-CY5bU4OqiJRox7G00sftw.gif'
@@ -16,79 +16,64 @@ interface AutoSizingImageProps extends FastImageProps {
1616 style ?: any
1717}
1818
19- interface AutoSizingImageState {
20- height : number
21- width : number
22- }
23-
24- class AutoSizingImage extends Component <
25- AutoSizingImageProps ,
26- AutoSizingImageState
27- > {
28- state = {
19+ const AutoSizingImage = ( props : AutoSizingImageProps ) => {
20+ const [ dimensions , setDimensions ] = useState ( {
2921 height : 0 ,
3022 width : 0 ,
31- }
23+ } )
3224
33- onLoad = ( e : any ) => {
34- const {
35- nativeEvent : { width, height } ,
36- } = e
37- this . setState ( { width, height } )
38- if ( this . props . onLoad ) {
39- this . props . onLoad ( e )
40- }
41- }
25+ const propsOnLoad = props . onLoad
26+ const onLoad = useCallback (
27+ ( e : any ) => {
28+ const {
29+ nativeEvent : { width, height } ,
30+ } = e
31+ setDimensions ( { width, height } )
32+ if ( propsOnLoad ) {
33+ propsOnLoad ( e )
34+ }
35+ } ,
36+ [ propsOnLoad ] ,
37+ )
4238
43- getHeight = ( ) => {
44- if ( ! this . state . height ) {
45- return this . props . defaultHeight === undefined
46- ? 300
47- : this . props . defaultHeight
39+ const height = useMemo ( ( ) => {
40+ if ( ! dimensions . height ) {
41+ return props . defaultHeight === undefined ? 300 : props . defaultHeight
4842 }
49- const ratio = this . state . height / this . state . width
50- const height = this . props . width * ratio
51- return height
52- }
53-
54- render ( ) {
55- const height = this . getHeight ( )
56- return (
57- < FastImage
58- { ...this . props }
59- onLoad = { this . onLoad }
60- style = { [ { width : this . props . width , height } , this . props . style ] }
61- />
62- )
63- }
43+ const ratio = dimensions . height / dimensions . width
44+ return props . width * ratio
45+ } , [ dimensions . height , dimensions . width , props . defaultHeight , props . width ] )
46+ return (
47+ < FastImage
48+ { ...props }
49+ onLoad = { onLoad }
50+ style = { [ { width : props . width , height } , props . style ] }
51+ />
52+ )
6453}
6554
66- interface AutoSizeExampleProps {
67- onPressReload : ( ) => void
68- bust : boolean
55+ export const AutoSizeExample = ( ) => {
56+ const { bust, url } = useCacheBust ( GIF_URL )
57+ return (
58+ < View >
59+ < Section >
60+ < FeatureText text = "• AutoSize." />
61+ </ Section >
62+ < SectionFlex onPress = { bust } >
63+ < AutoSizingImage
64+ style = { styles . image }
65+ width = { 200 }
66+ source = { { uri : url } }
67+ />
68+ </ SectionFlex >
69+ </ View >
70+ )
6971}
7072
71- const AutoSizeExample = ( { onPressReload, bust } : AutoSizeExampleProps ) => (
72- < View >
73- < Section >
74- < FeatureText text = "• AutoSize." />
75- </ Section >
76- < SectionFlex onPress = { onPressReload } >
77- < AutoSizingImage
78- style = { styles . image }
79- width = { 200 }
80- source = { { uri : GIF_URL + bust } }
81- />
82- </ SectionFlex >
83- </ View >
84- )
85-
8673const styles = StyleSheet . create ( {
8774 image : {
8875 backgroundColor : '#ddd' ,
8976 margin : 20 ,
9077 flex : 0 ,
9178 } ,
9279} )
93-
94- export default withCacheBust ( AutoSizeExample )
0 commit comments