Predict a greeting component's output
Read your first component
A function component describes a piece of UI by returning a React-renderable value. Begin with a function that returns JSX. JSX resembles HTML but is written inside JavaScript. A configured JSX transform is needed before a browser can execute it as JavaScript.
export default function Welcome() { return <p>Welcome to the reading room.</p>;}Expected display: a paragraph saying “Welcome to the reading room.” A component must be used by a React application before it appears; merely defining or exporting this function does not display it. Setup comes in Chapter 3.
Insert a value into JSX
A variable is a name for a value. const declares a binding that cannot be reassigned. When you place a JavaScript expression inside braces in JSX, React uses its value in the UI description.
export default function Welcome() { const learner = "Morgan"; return <p>Hello, {learner}!</p>;}Expected display: “Hello, Morgan!” The braces are not printed. If you edit the value to "Avery" and run the revised component in a configured application, it describes “Hello, Avery!” instead. This is a code edit, not a state update. A local constant alone does not create interactivity.
Reuse a component
A capitalized JSX tag such as <Welcome /> refers to a component in scope; a lowercase tag such as <p> refers to a built-in element. You can reuse the same component in more than one location.
function ReadingTip() { return <p>Read one page at a time.</p>;}
export default function App() { return ( <main> <h2>Reading room</h2> <ReadingTip /> <ReadingTip /> </main> );}Expected display: a heading reading “Reading room”, followed by two paragraphs with the same tip. The containing main element groups the content. The repeated paragraphs are intentional here to demonstrate reuse; in a real reading app each section would need a purpose.
Common mistakes
Writing <p>learner</p> displays the literal word learner; write <p>{learner}</p> to use the variable value. Writing <welcome /> does not refer to a function named Welcome. Match the capitalized name exactly. Finally, JSX is not directly supported by ordinary browser JavaScript without appropriate processing.
Practice and next step
Predict the result of <p>Topic: {topic}</p> when topic is "Space". Then predict the output when the same component appears twice. In the next lesson, decide when reusable React components are worth adding to a page.