@@ -33,16 +33,11 @@ export interface CommonLazyImageProps {
33
33
/** Valid props for LazyImageFull */
34
34
export interface LazyImageFullProps extends CommonLazyImageProps {
35
35
/** Children should be either a function or a node */
36
- children ?:
37
- | ( ( args : LazyImageFullRenderPropArgs ) => React . ReactNode )
38
- | React . ReactNode ;
39
-
40
- /** Render prop boolean indicating inView state */
41
- render ?: ( args : LazyImageFullRenderPropArgs ) => React . ReactNode ;
36
+ children : ( args : RenderCallbackArgs ) => React . ReactNode ;
42
37
}
43
38
44
39
/** Values that the render props take */
45
- export interface LazyImageFullRenderPropArgs {
40
+ export interface RenderCallbackArgs {
46
41
imageState : ImageState ;
47
42
src ?: string ;
48
43
srcSet ?: string ;
@@ -102,8 +97,6 @@ export class LazyImageFull extends React.Component<
102
97
// Bind methods
103
98
// This would be nicer with arrow functions and class properties,
104
99
// but holding off until they are settled.
105
- this . renderInner = this . renderInner . bind ( this ) ;
106
- this . renderLazy = this . renderLazy . bind ( this ) ;
107
100
this . onInView = this . onInView . bind ( this ) ;
108
101
this . onLoadSuccess = this . onLoadSuccess . bind ( this ) ;
109
102
this . onLoadError = this . onLoadError . bind ( this ) ;
@@ -114,13 +107,13 @@ export class LazyImageFull extends React.Component<
114
107
if ( inView === true ) {
115
108
// If src is not specified, then there is nothing to preload; skip to Loaded state
116
109
if ( ! this . props . src ) {
117
- this . setState ( ( state , props ) => ( {
110
+ this . setState ( ( state , _props ) => ( {
118
111
...state ,
119
112
imageState : ImageState . LoadSuccess
120
113
} ) ) ;
121
114
} else {
122
115
// Kick off request for Image and attach listeners for response
123
- this . setState ( ( state , props ) => ( {
116
+ this . setState ( ( state , _props ) => ( {
124
117
...state ,
125
118
imageState : ImageState . Loading
126
119
} ) ) ;
@@ -153,47 +146,25 @@ export class LazyImageFull extends React.Component<
153
146
154
147
// Render function
155
148
render ( ) {
156
- if ( this . props . loadEagerly ) {
149
+ const { children, loadEagerly, observerProps, ...cbProps } = this . props ;
150
+
151
+ if ( loadEagerly ) {
157
152
// If eager, skip the observer and view changing stuff; resolve the imageState as loaded.
158
- return this . renderInner ( this . props , {
159
- imageState : ImageState . LoadSuccess
160
- } ) ;
153
+ return children ( { imageState : ImageState . LoadSuccess , ...cbProps } ) ;
161
154
} else {
162
- return this . renderLazy ( this . props , this . state ) ;
155
+ return (
156
+ < Observer
157
+ rootMargin = "50px 0px"
158
+ threshold = { 0.01 }
159
+ { ...observerProps }
160
+ onChange = { this . onInView }
161
+ triggerOnce
162
+ >
163
+ { children ( { imageState : this . state . imageState , ...cbProps } ) }
164
+ </ Observer >
165
+ ) ;
163
166
}
164
167
}
165
-
166
- // TODO: split out
167
- renderLazy ( props : LazyImageFullProps , state : { imageState : ImageState } ) {
168
- const { observerProps } = props ;
169
-
170
- return (
171
- < Observer
172
- rootMargin = "50px 0px"
173
- threshold = { 0.01 }
174
- { ...observerProps }
175
- onChange = { this . onInView }
176
- triggerOnce
177
- >
178
- { this . renderInner ( props , state ) }
179
- </ Observer >
180
- ) ;
181
- }
182
-
183
- renderInner ( props : LazyImageFullProps , state : { imageState : ImageState } ) {
184
- const { src, srcSet, alt, sizes, render, children } = props ;
185
- const { imageState } = state ;
186
-
187
- return (
188
- < React . Fragment >
189
- { typeof render === "function"
190
- ? render ( { src, srcSet, alt, sizes, imageState } )
191
- : typeof children === "function"
192
- ? children ( { src, srcSet, alt, sizes, imageState } )
193
- : null }
194
- </ React . Fragment >
195
- ) ;
196
- }
197
168
}
198
169
199
170
// Utilities
0 commit comments