This repository has been archived by the owner on Jul 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
61 lines (50 loc) · 1.58 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
console.clear();
class Chat extends React.Component {
constructor(props) {
super(props);
this.state = {
messages: [{
who: 'them',
text: 'Welcome'
}]
}
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
const newMessages = [{
who: 'me',
text: event.target.message.value
}];
if (this.state.messages.length % 3 === 0) {
newMessages.push({
who: 'them',
text: "That's pretty neat!"
});
}
this.setState((previousState) => ({
messages: previousState.messages.concat(newMessages)
}), () => {
const messagesElement = document.querySelector('.messages');
messagesElement.scrollTop = messagesElement.scrollHeight + 500;
});
event.target.reset();
}
render() {
return (
<div className="chat">
<h1>senssense-chess chat</h1>
<div className="messages">
{this.state.messages.map((message) => <div className={`message ${message.who}`}>{message.text}</div>)}
</div>
<form onSubmit={this.handleSubmit}>
<input name="message" placeholder="Send message" autoComplete="off"/>
</form>
</div>
);
}
}
ReactDOM.render(
<Chat/>,
document.getElementById('app')
);