LearnThatStack Ace your next interview
React · question
Question 5 of 103

How do you create a React component?

beginner
← All React questions
Re-explain

Write a JavaScript function that returns JSX, and give it a name that starts with a capital letter. React calls it with a props object, and you use it in JSX like an HTML tag, such as <Welcome name="Sara" />.

There are two main ways to write one:

  1. A function component, the recommended way. You can declare it with the function keyword or as an arrow function.
  2. A class component, which extends React.Component and returns JSX from render(). It's the older style, and React still supports it.
function Welcome(props) {
  return <h1>Hello, {props.name}!</h1>;
}

// Or using arrow function
const Welcome = (props) => {
  return <h1>Hello, {props.name}!</h1>;
};
class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}!</h1>;
  }
}
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for React One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Why does a component name have to start with a capital letter? JSX reads lowercase tags as built-in HTML elements and capitalized tags as variables in scope, so a lowercase name never reaches your function.
  • How are props different from state? Props come from the parent and are read-only. State belongs to the component, and updating it triggers a re-render.
  • How would you give the function version of Welcome its own state? Call useState at the top level of the function, read the value, and change it only through the setter it returns.
  • Your component needs to return two sibling elements. How do you do that? Wrap them in a Fragment, written <>...</>, so no extra DOM node is added. An array with keys also works.
  • Is there anything a class component can still do that a function component can't? Name error boundaries and the class lifecycle methods they need, since no hook replaces them.

What you can say

  1. A React component is a JavaScript function that returns JSX, and its name starts with a capital letter.
  2. React calls that function with one props object, so for <Welcome name="Sara" /> the function gets props.name set to Sara.
  3. I use it in JSX like an HTML tag, and I can declare the function with the function keyword or as an arrow function. Either works.
  4. The older way is a class component that extends React.Component, returns JSX from render(), and reads props from this.props.
  5. For new code I write function components, since that's what React recommends. Class components still work, so I mostly see them in older codebases.

Weak answers to avoid

  • Leads with class components as the default React still supports classes, but it recommends function components. Lead with a function that returns JSX and call classes the older style.
  • Lowercase component name JSX treats <welcome /> as an HTML tag, so your component never renders. Say the capital-letter rule out loud and show it in the example.
  • Calls the component as a plain function Writing Welcome(props) instead of <Welcome /> means React doesn't treat it as its own component, and its hooks attach to the parent. Render it as a JSX tag.
  • Shows syntax but never mentions props Interviewers want to hear how data gets in. Say React passes one props object and use a prop in your example.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the React cheatsheet.

← Back to all React questions
Pro · $10/mo

88 of 103 React answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime