React and JSX

React components are reusable functions for creating interactive user interfaces.

Below is a simple React button component that counts clicks.

See the code for the button on GitHub.

See how this component is imported and used in THIS page.

The Code

Here is the JavaScript code that creates the button. The HTML-like syntax is called JSX. It can describe HTML in JavaScript code.

import { useState } from 'react'

function ExampleButton() {
  const [count, setCount] = useState(0)

  const handleClick = () => {
    setCount(count + 1)
  }

  return (
    <button
      onClick={handleClick}
      className="px-4 py-2 rounded-lg text-white bg-green-600 active:bg-green-700"
    >
      Clicked {count} Times
    </button>
  )
}

// Exporting the function makes it available to import in other files
export default ExampleButton