@@ -5,12 +5,6 @@ import Context from './Context';
55import EventSpy from '../EventSpy' ;
66import SpineTo from '../SpineTo' ;
77
8- function isBottom ( current , threshold ) {
9- const { offsetHeight, scrollHeight, scrollTop } = current ;
10-
11- return scrollHeight - scrollTop - offsetHeight <= threshold ;
12- }
13-
148export default class ScrollToBottomComposer extends React . Component {
159 constructor ( props ) {
1610 super ( props ) ;
@@ -26,21 +20,25 @@ export default class ScrollToBottomComposer extends React.Component {
2620 scrollTop : this . state . target && this . state . target . current && ( this . state . target . current . scrollHeight - this . state . target . current . offsetHeight )
2721 } ) ) ,
2822 scrollTop : null ,
29- setTarget : target => this . setState ( ( ) => ( { target } ) ) ,
30- target : null
23+ target : React . createRef ( )
3124 } ;
3225 }
3326
3427 handleScroll ( ) {
35- this . setState ( ( ) => ( {
36- bottom : isBottom ( this . state . target . current , this . props . threshold )
37- } ) ) ;
28+ this . setState ( ( ) => {
29+ const { current } = this . state . target ;
30+
31+ if ( current ) {
32+ const { offsetHeight, scrollHeight, scrollTop } = current ;
33+ const bottom = scrollHeight - scrollTop - offsetHeight <= this . props . threshold ;
34+
35+ return { bottom } ;
36+ }
37+ } ) ;
3838 }
3939
4040 handleScrollEnd ( ) {
41- this . setState ( ( ) => ( {
42- scrollTop : null
43- } ) ) ;
41+ this . setState ( ( ) => ( { scrollTop : null } ) ) ;
4442 }
4543
4644 render ( ) {
@@ -50,15 +48,16 @@ export default class ScrollToBottomComposer extends React.Component {
5048 < EventSpy
5149 name = "scroll"
5250 onEvent = { this . handleScroll }
53- target = { this . state . target && this . state . target . current }
51+ target = { this . state . target . current }
5452 />
55- { typeof this . state . scrollTop === 'number' &&
56- < SpineTo
57- name = "scrollTop"
58- onEnd = { this . handleScrollEnd }
59- target = { this . state . target && this . state . target . current }
60- value = { this . state . scrollTop }
61- />
53+ {
54+ typeof this . state . scrollTop === 'number' &&
55+ < SpineTo
56+ name = "scrollTop"
57+ onEnd = { this . handleScrollEnd }
58+ target = { this . state . target . current }
59+ value = { this . state . scrollTop }
60+ />
6261 }
6362 </ Context . Provider >
6463 ) ;
0 commit comments