scrimba
Learn class components in React
Class components practice
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Class components practice
AboutCommentsNotes
Class components practice
Expand for more info
index.js
run
preview
console

import React from "react"
import ReactDOM from "react-dom"

/*

Challenge:
1. Convert all 3 components to be class-based
2. Fix the small bug

*/

// #1
function App() {
return (
<div>
<Header />
<Greeting />
</div>
)
}

// #2
function Header(props) {
return (
<header>
<p>Welcome, {props.username}!</p>
</header>
)
}

// #3
// Hint: any "display logic" can be placed inside the `render`
// method before the `return` statement
function Greeting() {
const date = new Date()
const hours = date.getHours()
let timeOfDay

if (hours < 12) {
timeOfDay = "morning"
} else if (hours >= 12 && hours < 17) {
timeOfDay = "afternoon"
} else {
timeOfDay = "night"
}
return (
<h1>Good {timeOfDay} to you, sir or madam!</h1>
)
}

ReactDOM.render(<App />, document.getElementById("root"))
Console
/index.html
-3:10