首页 > 解决方案 > 写这样的表达式 const [count, setCount] = useState(0) 是什么意思

问题描述

当我偶然发现这段代码时,我正在阅读 React hooks。

import React, { useState, useEffect } from 'react';

function Example() {
  const [count, setCount] = useState(0);

  // Similar to componentDidMount and componentDidUpdate
  useEffect(() => {
    // Update the document title using the browser API
    document.title = `You clicked ${count} times`;
  });

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

我试图了解这个声明const [count, setCount] = useState(0);后来是如何以这种方式使用的setCount(count + 1)

标签: javascriptreactjs

解决方案


推荐阅读