Use React Hooks in an Element Block
Write a Block Using React Hooks
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 React Hooks in an Element Block
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.
- Update your import statement for React to also import the
useState
hook:
import React, { useState } from 'react'
- Replace the block component with the following implementation, which uses
useState
to keep track of a statefulcount
variable:
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.