main.jsx, which mounts your app onto the page, and App.jsx, your very first real component.Nearly all modern React components are simply JavaScript functions that return JSX โ markup-like syntax describing what should appear on screen:
function App() {
return <h1>Hello, React!</h1>;
}
export default App;function App(): a normal JavaScript function โ but by React convention, component names always start with a capital letterreturn <h1>...</h1>: returns JSX describing the UIexport default App: makes this component importable from other files
main.jsx is the true entry point โ it takes your App component and renders it into an actual DOM element on the page:
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";
createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>
);document.getElementById("root") refers to a single empty <div id="root"> sitting in index.html โ createRoot().render() is the bridge connecting your React component tree to that real DOM node.
Every component lives in its own file and is shared using export / import:
// Button.jsx
function Button() {
return <button>Click Me</button>;
}
export default Button;
// App.jsx
import Button from "./Button.jsx";
function App() {
return <Button />;
}
Creating a component but forgetting export default ComponentName at the bottom of the file means any other file trying to import it will get undefined instead of your actual component โ a very common early-beginner error that produces a confusing blank screen with no clear error message.
Create a new Greeting component in its own file that returns a heading with your name, then import and use it inside App.jsx.
// Greeting.jsx
function Greeting() {
return <h2>Hello, I'm learning React!</h2>;
}
export default Greeting;
// App.jsx
import Greeting from "./Greeting.jsx";
function App() {
return <Greeting />;
}
export default App;
Q Why must component names start with a capital letter?
React uses this exact convention to distinguish components (like
Q What is the in index.html for?
It's the single empty container element in your plain HTML file that React takes over completely. Everything your React app renders is inserted inside this one div.
It's the single empty container element in your plain HTML file that React takes over completely. Everything your React app renders is inserted inside this one div.