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.
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
className strings? These use Tailwind CSS for styling colors, formatting, and more.px-4 add left and right paddingpy-2 add top and bottom paddingrounded-lg large rounded cornerstext-white color the textbg-green-600 color the backgroundactive:bg-green-700 make the background darker when when "active" (when the button is being pressed)const [count, setCount] = useState(0)?