Skip to content

Instantly share code, notes, and snippets.

@bookercodes
Last active June 4, 2018 16:10
Show Gist options
  • Save bookercodes/cc971d7e66950703f6d4384d2d73d458 to your computer and use it in GitHub Desktop.
Save bookercodes/cc971d7e66950703f6d4384d2d73d458 to your computer and use it in GitHub Desktop.
// ./src/App.js
import React, { Component } from 'react'
import UsernameForm from './UsernameForm'
+import Chat from './Chat'
class App extends Component {
state = {
currentUsername: null,
- currentId: null
+ currentId: null,
+ currentScreen: 'usernameForm'
}
onUsernameSubmitted = username => {
fetch('http://localhost:3001/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username })
})
.then(response => response.json())
.then(data => {
this.setState({
currentId: data.id,
- currentUsername: data.name
+ currentUsername: data.name,
+ currentScreen: 'chat'
})
})
.catch(error => {
console.error('error', error)
})
}
render() {
- return <UsernameForm handleSubmit={this.onUsernameSubmitted} />
+ if (this.state.currentScreen === 'usernameForm') {
+ . return <UsernameForm handleSubmit={this.onUsernameSubmitted} />
+ }
+ if (this.state.currentScreen === 'chat') {
+ return <Chat currentId={this.state.currentId} />
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment