Skip to content
tezvyn:

What are the two main ways to define a component in React?

Source: react.devEasyHow cards are made

What are the two main ways to define a component in React?

Tests fluency in function and class syntax. Outline: show a function returning an h1; show a class with render() returning an h1; note functions are the React 19 default. Red flag: offering arrow vs function declaration as the two ways, or omitting render().

What's really being asked

This question checks whether you can articulate the two primary component paradigms in React and write syntactically correct minimal examples from memory. For a senior candidate, the interviewer cares less about the syntax itself and more about whether you understand that function components are the modern React 19 standard while class components are still valid and prevalent in legacy codebases. It also tests JSX literacy, capitalization rules, and whether you know the minimal API surface for each type.

The full answer

Four things in order. First, identify the two main ways as function components and class components. Second, provide a function component example such as function Hello() { return <h1>Hi</h1>; } or an arrow function equivalent, emphasizing that the name must start with a capital letter. Third, provide a class component example such as class Hello extends React.Component { render() { return <h1>Hi</h1>; } }, explicitly including the render method because classes require it. Fourth, contextualize by stating that function components are now the default for new development because they support Hooks and align with React 19 architecture, but class components are not deprecated and seniors often maintain them.

The mistakes people make

Several patterns signal inexperience. Offering function declarations versus arrow functions as the two main ways misses the point entirely; those are both function components. Forgetting the render method in a class component is a syntax error that breaks React. Returning multiple top level elements without a fragment in either example shows poor JSX hygiene. Claiming that class components are deprecated or removed in React 19 is factually wrong. Failing to capitalize the component name is a basic mistake that causes React to treat it as a DOM tag.

What usually comes next

An interviewer might ask when you would still use a class component today. The honest answer is rarely for new code, though you may encounter them in older codebases. They might ask how state or side effects differ between the two, which lets you discuss Hooks versus lifecycle methods. They might also ask about converting a class to a function, testing differences, or performance characteristics.

A concrete example

Function component: function Hello() { return <h1>Hello</h1>; } export default Hello; Class component: import React from 'react'; class Hello extends React.Component { render() { return <h1>Hello</h1>; } } export default Hello; Both render an h1 tag and satisfy the minimal requirement, but the function version is what you would write in a greenfield React 19 application.

Interview question

Which accurately describes the two primary ways to define a React component?

  • a.Arrow functions and function declarations, with arrow functions being the modern standard
  • b.Components defined with state and components defined without state, which dictates the syntax required
  • c.Function components and class components, with function components being the standard for new React 19 codeCorrect
  • d.Class components and function components, but class components are deprecated and removed in React 19
Why?

Function components and class components are the two distinct definition paradigms, and function components are the React 19 default. Arrow functions and function declarations are merely alternate syntaxes for the same function component paradigm, not separate ways to define components.

Just read this? Test yourself on what you have been reading.

Read the original → react.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles