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

0 votes
51 views
added Mar 12, 2021 in React by lcjr First Warrant Officer (9,190 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 (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...