Skip to content

Commit 1f1e648

Browse files
committed
Move from createRef to callback ref
1 parent 045b2ce commit 1f1e648

File tree

4 files changed

+23
-16
lines changed

4 files changed

+23
-16
lines changed

packages/component/src/BasicScrollToBottom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { css } from 'glamor';
22
import classNames from 'classnames';
33
import React from 'react';
44

5+
import AutoHideFollowButton from './ScrollToBottom/AutoHideFollowButton';
56
import Composer from './ScrollToBottom/Composer';
67
import Context from './ScrollToBottom/Context';
7-
import AutoHideFollowButton from './ScrollToBottom/AutoHideFollowButton';
88
import Panel from './ScrollToBottom/Panel';
99

1010
const ROOT_CSS = css({

packages/component/src/ScrollToBottom/Composer.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ export default class ScrollToBottomComposer extends React.Component {
1515
this.state = {
1616
bottom: true,
1717
handleUpdate: () => this.state.bottom && this.state.scrollToBottom(),
18-
scrollToBottom: () => this.setState(() => ({
19-
scrollTop: this.state.target && this.state.target.current && (this.state.target.current.scrollHeight - this.state.target.current.offsetHeight)
18+
scrollToBottom: () => this.setState(state => ({
19+
scrollTop: state.target && state.target && (state.target.scrollHeight - state.target.offsetHeight)
2020
})),
2121
scrollTop: null,
22-
target: React.createRef()
22+
setTarget: target => this.setState(() => ({ target })),
23+
target: null,
2324
};
2425
}
2526

2627
handleScroll() {
2728
this.setState(() => {
28-
const { current } = this.state.target;
29+
const { target } = this.state;
2930

30-
if (current) {
31-
const { offsetHeight, scrollHeight, scrollTop } = current;
31+
if (target) {
32+
const { offsetHeight, scrollHeight, scrollTop } = target;
3233
const bottom = scrollHeight - scrollTop - offsetHeight <= this.props.threshold;
3334

3435
return { bottom };
@@ -41,21 +42,26 @@ export default class ScrollToBottomComposer extends React.Component {
4142
}
4243

4344
render() {
45+
const { scrollTop, target } = this.state;
46+
4447
return (
4548
<Context.Provider value={ this.state }>
4649
{ this.props.children }
47-
<EventSpy
48-
name="scroll"
49-
onEvent={ this.handleScroll }
50-
target={ this.state.target.current }
51-
/>
5250
{
53-
typeof this.state.scrollTop === 'number' &&
51+
target &&
52+
<EventSpy
53+
name="scroll"
54+
onEvent={ this.handleScroll }
55+
target={ target }
56+
/>
57+
}
58+
{
59+
target && typeof scrollTop === 'number' &&
5460
<SpineTo
5561
name="scrollTop"
5662
onEnd={ this.handleScrollEnd }
57-
target={ this.state.target.current }
58-
value={ this.state.scrollTop }
63+
target={ target }
64+
value={ scrollTop }
5965
/>
6066
}
6167
</Context.Provider>

packages/component/src/ScrollToBottom/Context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export default React.createContext({
55
handleUpdate: () => 0,
66
scrollToBottom: () => 0,
77
scrollTop: null,
8+
setTarget: () => 0,
89
target: null
910
});

packages/component/src/ScrollToBottom/Panel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class ScrollToBottomPanel extends React.PureComponent {
2626
return (
2727
<div
2828
className={ classNames(ROOT_CSS + '', props.className) }
29-
ref={ context.target }
29+
ref={ context.setTarget }
3030
>
3131
{ props.children }
3232
</div>

0 commit comments

Comments
 (0)