Created
October 6, 2018 10:11
-
-
Save alqamabinsadiq/73e37a2a604aa93840f77d99dbcd2b2c to your computer and use it in GitHub Desktop.
Migrating From Unsafe React Lifecycles Hooks - ReactKHI meetup #01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import Message from './message'; | |
import { getMessage } from './getMessage'; | |
class Chat extends Component { | |
// Initial State. | |
state = { | |
messages: [getMessage()], | |
isStreaming: this.props.isStreaming | |
} | |
// Message Container. | |
static MessageContainer = ({ messages, ulRef }) => { | |
return ( | |
<ul ref={ulRef} className="chatBox" > | |
{ | |
messages.map((message, index) => { | |
return <Message {...message} key={index} /> | |
}) | |
} | |
</ul> | |
) | |
} | |
// Header of application. | |
static Header = () => { | |
return ( | |
<div className="Chat-legend"> | |
Chat Messages | |
</div> | |
); | |
} | |
componentDidMount() { | |
this.timerID = setInterval(() => { | |
if (this.state.isStreaming) { | |
this.generateMessage(); | |
} | |
}, 1000); | |
document.addEventListener("keydown", this.handleKey); | |
} | |
static getDerivedStateFromProps(props, state) { | |
if (props.isStreaming !== state.isStreaming) { | |
return { | |
isStreaming: props.isStreaming | |
}; | |
} | |
return null; | |
} | |
getSnapshotBeforeUpdate(prevProps, prevState) { | |
const { current } = this.ulRef; | |
return { | |
shouldAutoScroll: | |
current.scrollTop + current.clientHeight >= | |
current.scrollHeight | |
}; | |
} | |
componentDidUpdate(prevProps, prevState, { shouldAutoScroll }) { | |
if (shouldAutoScroll) { | |
const { current } = this.ulRef; | |
current.scrollTop = current.scrollHeight; | |
} | |
} | |
componentWillUnmount() { | |
clearInterval(this.timerID); | |
document.removeEventListener("keydown", this.handleKey); | |
} | |
// fetch the new messages. | |
generateMessage = () => { | |
const message = getMessage(); | |
this.setState(prevState => ({ | |
messages: [...prevState.messages, message] | |
})); | |
}; | |
// Handles the keystroke. | |
handleKey = e => { | |
if (e.key === "c") { | |
this.setState({ messages: [getMessage()] }); | |
} | |
}; | |
// Ref of ul | |
ulRef = React.createRef(); | |
// Main Render | |
render() { | |
return ( | |
<div> | |
<Chat.MessageContainer messages={this.state.messages} ulRef={this.ulRef} /> | |
</div> | |
); | |
} | |
} | |
export default Chat; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import Message from './message'; | |
import { getMessage } from './getMessage'; | |
class Chat extends Component { | |
// Initial State. | |
state = { | |
messages: [getMessage()], | |
isStreaming: this.props.isStreaming | |
} | |
// Message Container. | |
static MessageContainer = ({ messages, ulRef }) => { | |
return ( | |
<ul ref={ulRef} className="chatBox" > | |
{ | |
messages.map((message, index) => { | |
return <Message {...message} key={index} /> | |
}) | |
} | |
</ul> | |
) | |
} | |
// Header of application. | |
static Header = () => { | |
return ( | |
<div className="Chat-legend"> | |
Chat Messages | |
</div> | |
); | |
} | |
UNSAFE_componentWillMount() { | |
this.timerID = setInterval(() => { | |
if (this.state.isStreaming) { | |
this.generateMessage(); | |
} | |
}, 1000); | |
document.addEventListener("keydown", this.handleKey); | |
} | |
UNSAFE_componentWillReceiveProps(nextProps) { | |
if (nextProps.isStreaming !== this.state.isStreaming) { | |
this.setState({ | |
isStreaming: nextProps.isStreaming | |
}); | |
} | |
} | |
UNSAFE_componentWillUpdate(nextProps, nextState) { | |
const { current } = this.ulRef; | |
this.shouldAutoScroll = | |
current.scrollTop + current.clientHeight >= | |
current.scrollHeight; | |
} | |
componentDidUpdate(prevProps, prevState) { | |
if (this.shouldAutoScroll) { | |
console.log('hello'); | |
const { current } = this.ulRef; | |
current.scrollTop = current.scrollHeight; | |
} | |
} | |
componentWillUnmount() { | |
clearInterval(this.timerID); | |
document.removeEventListener("keydown", this.handleKey); | |
} | |
// fetch the new messages. | |
generateMessage = () => { | |
const message = getMessage(); | |
this.setState(prevState => ({ | |
messages: [...prevState.messages, message] | |
})); | |
}; | |
// Handles the keystroke. | |
handleKey = e => { | |
if (e.key === "c") { | |
this.setState({ messages: [getMessage()] }); | |
} | |
}; | |
// Ref of ul | |
ulRef = React.createRef(); | |
// Main Render | |
render() { | |
return ( | |
<div> | |
<Chat.MessageContainer messages={this.state.messages} ulRef={this.ulRef} /> | |
</div> | |
); | |
} | |
} | |
export default Chat; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Both the componentWillReceiveProps and the static getDerivedStateFromProps() add significant complexity to the component. See this blog post: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#what-about-memoization\