Component Files
Component files keep each component in its own file. One file holds one component in most small apps.
Export a component
Greeting leaves its file through a default export.
export default function Greeting() {
return <h1>Hello from Greeting!</h1>
}Import a component
App brings Greeting in from its file.
import Greeting from "./Greeting.jsx"Use the imported component
The tag looks the same as a local component.
function App() {
return <Greeting />
}Example
function Greeting() {
return <h1>Hello from Greeting!</h1>
}
export default function App() {
return <Greeting />
}
Key points
- A component file ends with .jsx in a Vite app.
- A default export makes one component available to other files.
- App imports the component from its file path.
- The import path starts with ./ for a file in the same folder.
In this example, Greeting returns a heading. App returns Greeting. The two parts sit in one file here. In a real project, Greeting lives in Greeting.jsx. App lives in App.jsx. App imports Greeting from ./Greeting.jsx.
Note
A small app can keep two components in one file. The split still helps as the app grows.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does App use to bring in a component from another file?