In this how-to guide, we will go over how to use React hooks in your blocks.
React hooks allow you add a state to React functional components without using a class. You can read more about it in the official documentation.
Using hooks in an Element block is the same as using them in any other React component. Here, we'll be modifying the Starter Block to create a stateful component using React hooks.
import React, { useState } from 'react'
function StarterBlock(props) {
const [count, setCount] = useState(0)
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
)
}
For more details about how and why to use hooks, refer to React's official documentation.