React: Concept: Hook: useState

1st March 2021 at 9:49pm
React: Concept: Hook

对于这样一个例子:

import React, { useState } from 'react';

function Example() {
  const [count1, setCount1] = useState(0);
  const [count2, setCount2] = useState(0);

  return (
    <div>
      <p>Count1: {count1} times</p>
        <button onClick={() => setCount1(count1 + 1)}>
          Click me
        </button>

     <p>Count2: {count2} times</p>
       <button onClick={() => setCount2(count2 + 1)}>
         Click me
       </button>
    </div>
  );
}

function AnotherExample() {
  return (
    <>
      <Example />
      <Example />
    </>
  )
}

我做了个视频讲解: