A Vite React project begins with an HTML element whose id is root. The JavaScript entry file gives React control of that element and renders the top-level component.
The React root
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; const rootElement = document.getElementById( "root" ); const root = createRoot(rootElement); root.render( <App /> );
The App component
export default function App() { return ( <main> <h1>My tasks</h1> <p>Three tasks remaining</p> </main> ); }
Component rules
Component names begin with a capital letter so React can distinguish them from HTML elements. A component returns one root element; use a fragment, written <>...</>, when an extra DOM element would be meaningless. Keep components pure: calculate their output without changing values outside the component.
How the two files connect
Execution begins in main.jsx. The first import takes createRoot from React's browser package. The second follows the default export from App.jsx and gives it the local name App.
document.getElementById("root")finds<div id="root"></div>inindex.html.createRoot(...)creates React's connection to that DOM element.<App />asks React to render the imported component.- React calls
App, receives its JSX and creates the correspondingmain,h1andpelements.
The slash closes a component with no nested content. JSX lets React control when the function renders instead of you calling App() directly.
What you should see
The page shows a My tasks
heading followed by Three tasks remaining
. If it is blank, check the browser console. A misspelled root, missing export or incorrect filename usually produces a useful error.
Challenge: add a second component
Create a Footer function that returns a footer containing your name. Render it after main without adding a meaningless wrapper div.
Wrap main and Footer in a fragment: <>...</>.
function Footer() {
return <footer>Built by Jamie</footer>;
}
export default function App() {
return <>
<main><h1>My tasks</h1><p>Three tasks remaining</p></main>
<Footer />
</>;
}
