• Hotline 0125478963
  • info@thebrilliance.com


No products in the cart.

Learning React for Beginners

Function Components

We’ll now change the Square to be a function component.

In React, function components are a simpler way to write components that only contain a render method and don’t have their own state. Instead of defining a class which extends React.Component, we can write a function that takes props as input and returns what should be rendered. Function components are less tedious to write than classes, and many components can be expressed this way.

Replace the Square class with this function:

function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>

We have changed this.props to props both times it appears.

View the full code at this point


When we modified the Square to be a function component, we also changed onClick={() => this.props.onClick()} to a shorter onClick={props.onClick} (note the lack of parentheses on both sides).



Connect with: