UI as Tree Example React - Component
see https://react.dev/learn/understanding-your-ui-as-a-tree
by phollome
HTML
<!DOCTYPE html>
<html>
<head>
<title>Get inspired - React Component</title>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root" />
<script type="text/babel">
const quotes = [
"Don’t let yesterday take up too much of today.” — Will Rogers",
"Ambition is putting a ladder against the sky.",
"A joy that's shared is a joy made double.",
];
function Quote() {
const [index, setIndex] = React.useState(0);
const next = () => {
if (index >= quotes.length -1) {
setIndex(0);
} else {
setIndex(index + 1);
}
}
return (
<>
<h3 className="fancy cursive">{quotes[index]}</h3>
<button onClick={next}>Inspire me again</button>
</>
)
}
function App() {
const year = 2004;
return (
<>
<h1 className="fancy title">Get inspired - React Component</h1>
<p>Your inspirational quote is:</p>
<Quote />
<p id="copyright" className="small">©️ {year}</p>
</>
);
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
</script>
</body>
</html>
CSS
* {
box-sizing: border-box;
}
body {
font-family: sans-serif;
margin: 20px;
padding: 0;
}
h1 {
margin-top: 0;
font-size: 22px;
}
h2 {
margin-top: 0;
font-size: 20px;
}
h3 {
margin-top: 0;
font-size: 18px;
}
h4 {
margin-top: 0;
font-size: 16px;
}
h5 {
margin-top: 0;
font-size: 14px;
}
h6 {
margin-top: 0;
font-size: 12px;
}
code {
font-size: 1.2em;
}
ul {
padding-inline-start: 20px;
}
.fancy {
font-family: 'Georgia';
}
.title {
color: #007AA3;
text-decoration: underline;
}
.cursive {
font-style: italic;
}
.small {
font-size: 10px;
}