After the imports, we have a function named
App
. Whereas most of the JavaScript community prefers camel-case names like
helloWorld
, React components use pascal-case variable names, like
HelloWorld
, to make it clear that a given JSX element is a React component, and not a regular HTML tag. If you were to rename the
App
function to
app
, your browser would show you an error.
Let's look at
App
more closely.
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
Edit <code>src/App.js</code> and save to reload.
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer">
Learn React
</header>
The App
function returns a JSX expression. This expression defines what your browser ultimately renders to the DOM.
Some elements in the expression have attributes, which are written just like in HTML, following a pattern of attribute="value"
. On line 3, the opening <div>
tag has a className
attribute. This is the same as the class
attribute in HTML, but because JSX is JavaScript, we can't use the word class
— it's reserved, meaning JavaScript already uses it for a specific purpose and it would cause problems here in our code. A few other HTML attributes are written differently in JSX than they are in HTML too, for the same kind of reason. We'll cover them as we encounter them.
Take a moment to change the <p>
tag on line 6 so that it reads "Hello, world!", then save your file. You'll notice that this change is immediately rendered in the development server running at http://localhost:3000
in your browser. Now delete the <a>
tag and save; the "Learn React" link will be gone.
Your App
component should now look like this:
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>Hello, World!</p>
</header>