React hook - useState function to use state without writing a class

0 votes
added Mar 12, 2021 in React by lcjr First Warrant Officer (11,790 points)

Hooks is a new feature made available to React 16.8. They let us use state and other React features without writing a class.

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0);

  return (
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
} - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.