← React Beginner Questions - Set 6 Q60

What is a functional component in React?

React Beginner Questions - Set 6

A function that receives props and returns JSX. With hooks it holds state, effects and refs — the class distinction is historical.

Example

import { useState } from 'react';

// Props in, JSX out; state comes from hooks
export default function LikeButton({ initial = 0 }) {
  const [likes, setLikes] = useState(initial);

  return (
    <button onClick={() => setLikes(l => l + 1)} aria-pressed={likes > 0}>
      ♥ {likes}
    </button>
  );
}

// It is just a function: callable, composable, testable
// <LikeButton initial={5} />

What to notice

← Back to the question list