TL;DR
A function component is a regular JavaScript function that takes props as its first argument and returns JSX. It's how you define components in modern React, and hooks give it the ability to remember things and run code at specific times. You also need to recognize older class components when you see them.
- You can shorten a component with an arrow function and destructure props right in the parameters, like ({ message }). Everyone calls the first argument props, so stick with that name.
- Destructuring lets you set defaults, so { message = "Nothing to see here" } kicks in when nobody passes a message.
- A class component extends React.Component, returns JSX from a render method, and reads this.props. It may also use this.state, this.setState, and lifecycle methods like componentDidMount.
- Converting a class is mechanical: the render body becomes the function body, this.props becomes props, and state and lifecycle methods become useState and useEffect.
Let's face it. The components we will be writing aren't going to be changing the world. The bulk of the components we are going to write will do a few simple things, all of which revolve around taking some props and spitting out some markup. For components like these, you don't need anything fancy to define them. A plain old function will do just fine.
In the previous tutorial, we created our components using functions without making a big deal out of it. In the following sections, we are going to take a closer look at what makes function components tick and some of the neat tricks they allow. We'll also look at the older way of creating components, because you will run into it in the wild.
Onwards!
OMG! A React Book Written by Kirupa?!!
To kick your React skills up a few notches, everything you see here and more (with all its casual clarity!) is available in both paperback and digital editions.
BUY ON AMAZONGetting Back to Functions
Here is a component that takes a message and displays it:
function SimpleMessage(props) {
return <p>Message is: {props.message}!</p>;
}
This is what a function component looks like. It is a regular JavaScript function that takes in the properties passed to it and returns some JSX. Defining components this way looks ridiculously simple, and it totally works. The way you call this component is just like any other component. Here is the render call for our SimpleMessage component:
root.render(
<div>
<SimpleMessage message="Functions are back in style!" />
</div>
);
In this snippet, root is the object we get back from createRoot, just like we saw earlier. You may be wondering about components that need to remember things or run code at specific times. Function components can do both of those things (and more) thanks to a feature known as hooks. We'll look at hooks in a little bit. For now, all you need to know is that function components are the way you define components in modern React.
Function Components and their Arguments
In this function-based world, the props object is passed in as the first argument:
function HelloSomething(props) {
return <p>Hello, {props.target}!</p>;
}
This shouldn't be a total surprise, for we've already seen that in the snippets earlier. One detail to note is that you can set any name you want to refer to the first argument. It doesn't have to be props like we've seen so far. It can be something like jello:
function HelloSomething(jello) {
return <p>Hello, {jello.target}!</p>;
}
Everybody calls it props, though, so I recommend sticking with that name unless you have a good reason not to. Your teammates will thank you.
Kicking it up a Notch!
Because you are working with functions, you can go a little crazy and take advantage of the JavaScript techniques you have at your disposal. For example, if you are someone who finds the existence of the function keyword and return statement to be offensive, you can go even more compact and use arrow functions:
const SimpleMessage = (props) => <p>Message is: {props.message}!</p>;
The end result is the same. But why stop here? If you are not a fan of having to rely on your props object each time you want to access a property on it, you can take advantage of destructuring:
const SimpleMessage = ({ message }) => <p>Message is: {message}!</p>;
This pulls the message property right out of the props object, so you can use it directly. Destructuring works just as well with a regular function:
function SimpleMessage({ message }) {
return <p>Message is: {message}!</p>;
}
Destructuring also gives you a neat way of setting a default value for a property. If someone forgets to specify one, the default value you provide is used instead:
function SimpleMessage({ message = "Nothing to see here" }) {
return <p>Message is: {message}!</p>;
}
With this in place, calling <SimpleMessage /> without a message displays Message is: Nothing to see here!. There is more dark magic you can invoke to make our component definition even more compact, but you get the idea. Functions give you a lot of flexibility, and all of the JavaScript you already know works as-is.
Reading Class Components
Before function components became the standard, the way you defined components was by using a JavaScript class. You aren't going to write components this way, but there is a very good chance you will run into class components in older tutorials, in older code bases, and in some libraries. Here is what our SimpleMessage component looks like as a class:
class SimpleMessage extends React.Component {
render() {
return <p>Message is: {this.props.message}!</p>;
}
}
Here is how to make sense of what you see:
- The component extends React.Component. This is what makes it a component.
- Instead of returning JSX directly, the class has a render method that returns the JSX.
- Properties are accessed via this.props instead of a props argument.
Class components can do more than just display props. You'll also see things like a constructor, state stored in this.state and changed via this.setState, and lifecycle methods like componentDidMount that run code at specific points in a component's life. In a function component, state is handled by a hook called useState, and the work that lifecycle methods used to do is handled by a hook called useEffect. We'll look at both of those in upcoming tutorials.
Class components still work, so older code doesn't need to be thrown away. If you ever need to convert one, the translation from a class to a function is pretty mechanical: turn the render method into the function body, swap this.props for props, and replace state and lifecycle methods with hooks.
Conclusion
Function components are how you will be building everything from here on out. They are simple to write, easy to read, and they give you the full power of JavaScript for doing all sorts of things inside them. Besides function components being the recommended approach, there is another good reason to know them well: nearly every modern React example you will see in the wild uses them.
One more thing. Even though you won't be writing class components, you should be able to recognize them when you see them. Older examples may use them extensively, so you need to know how to read them and not get tripped up. This has been a public service announcement.
Next tutorial: Meet JSX...Again!
Before you go, here are a few things you can do:
-
Keep reading
Read the next article in the series, Meet JSX...Again!, or check out some related content:
-
Ask a question
Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇


