|
1 | | -import React, { Component } from 'react' |
| 1 | +import React, { useState } from 'react' |
2 | 2 | import Toggle from 'react-toggle' |
3 | 3 |
|
4 | | -import 'react-toggle/style.css' |
| 4 | +import ComponentExample from './ComponentExample' |
| 5 | +import HookExample from './HookExample' |
5 | 6 |
|
6 | | -import BottomScrollListener from 'react-bottom-scroll-listener' |
| 7 | +const App = () => { |
| 8 | + const [hookExample, setHookExample] = useState(true) |
| 9 | + const [alertOnBottom, setAlertOnBottom] = useState(true) |
7 | 10 |
|
8 | | -export default class App extends Component { |
9 | | - state = { |
10 | | - alertOnBottom: true, |
11 | | - } |
12 | | - |
13 | | - handleAlertBottomChange = () => { |
14 | | - this.setState(prevState => ({ alertOnBottom: !prevState.alertOnBottom })) |
15 | | - } |
16 | | - |
17 | | - handleOnDocumentBottom = () => { |
18 | | - console.log('I am at bottom! ' + Math.round(performance.now())) |
19 | | - |
20 | | - if (this.state.alertOnBottom) { |
21 | | - alert('Bottom hit! Too slow? Reduce "debounce" value in props') |
22 | | - } |
23 | | - } |
24 | | - |
25 | | - handleContainerOnBottom = () => { |
26 | | - console.log('I am at bottom in optional container! ' + Math.round(performance.now())) |
27 | | - |
28 | | - if (this.state.alertOnBottom) { |
29 | | - alert('Bottom of this container hit! Too slow? Reduce "debounce" value in props') |
30 | | - } |
31 | | - } |
32 | | - |
33 | | - render() { |
34 | | - return ( |
35 | | - <div className="root"> |
36 | | - <header> |
37 | | - <h1>react-bottom-scroll-listener</h1> |
| 11 | + return ( |
| 12 | + <div> |
| 13 | + <header> |
| 14 | + <h1>react-bottom-scroll-listener</h1> |
| 15 | + <div className="right-toggle-box"> |
38 | 16 | <label htmlFor="alert-state"> |
39 | | - <span>Use {this.state.alertOnBottom ? 'alert dialog' : 'console.log'}</span> |
40 | | - <Toggle id="alert-state" checked={this.state.alertOnBottom} onChange={this.handleAlertBottomChange} /> |
| 17 | + <span>Use {alertOnBottom ? 'alert dialog' : 'console.log'}</span> |
| 18 | + <Toggle id="alert-state" checked={alertOnBottom} onChange={() => setAlertOnBottom(b => !b)} /> |
| 19 | + </label> |
| 20 | + <label htmlFor="use-hook-state"> |
| 21 | + <span>Use {hookExample ? 'hook' : 'component'}</span> |
| 22 | + <Toggle id="use-hook-state" checked={hookExample} onChange={() => setHookExample(b => !b)} /> |
41 | 23 | </label> |
42 | | - </header> |
43 | | - |
44 | | - <div className="scrollbox"> |
45 | | - <h2>Callback when document hits bottom</h2> |
46 | | - <div>Scroll down! ▼▼▼</div> |
47 | | - <div>A bit more... ▼▼</div> |
48 | | - <div>Almost there... ▼</div> |
49 | | - <div>You've reached the bottom!</div> |
50 | 24 | </div> |
51 | | - |
52 | | - {/* When you only want to listen to the bottom of "document", you can put it anywhere */} |
53 | | - <BottomScrollListener onBottom={this.handleOnDocumentBottom} /> |
54 | | - |
55 | | - {/* If you want to listen for the bottom of a specific container you need to forward |
56 | | - a scrollRef as a ref to your container */} |
57 | | - <BottomScrollListener onBottom={this.handleContainerOnBottom}> |
58 | | - {scrollRef => ( |
59 | | - <div ref={scrollRef} className="innerScrollExample"> |
60 | | - <h4>Callback when this container hits bottom</h4> |
61 | | - <div>Scroll down! ▼▼▼</div> |
62 | | - <div>A bit more... ▼▼</div> |
63 | | - <div>Almost there... ▼</div> |
64 | | - <div>You've reached the bottom!</div> |
65 | | - </div> |
66 | | - )} |
67 | | - </BottomScrollListener> |
68 | | - </div> |
69 | | - ) |
70 | | - } |
| 25 | + </header> |
| 26 | + {hookExample ? <HookExample alertOnBottom={alertOnBottom} /> : <ComponentExample alertOnBottom={alertOnBottom} />} |
| 27 | + </div> |
| 28 | + ) |
71 | 29 | } |
| 30 | + |
| 31 | +export default App |
0 commit comments