I want to dynamically change the text and color of my h1 heading based on the time of day.
I'm encountering an error with my current code: "Cannot set property 'innerText' of null".
I've tried to figure out how to incorporate a function in the render section, but haven't had any luck.
This is what my code looks like:
import React from "react";
import ReactDOM from "react-dom";
const morning = { color: "blue" };
const afternoon = { color: "green" };
const night = { color: "grey" };
const date = new Date();
const currentHour = date.getHours();
function sayHello() {
const heading = document.querySelector("h1");
if (currentHour < 12) {
heading.innerText = "Good Morning!";
heading.style.color = morning;
} else if (currentHour > 12 && currentHour < 18) {
heading.innerText = "Good Afternoon!";
heading.style.color = afternoon;
} else {
heading.innerText = "Good Night!";
heading.style.color = night;
}
}
ReactDOM.render(
<h1 className="heading">{sayHello()}</h1>,
document.getElementById("root")
);