为什么我的 React 组件在状态更改后不重新渲染?

问题描述 投票:0回答:1

我正在构建一个简单的 React 应用程序,在单击按钮时更新状态。但是,当状态发生变化时,我的组件不会按预期重新渲染。这是我的代码:

import React, { useState } from 'react';

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

  const handleClick = () => {
    setCount(count + 1);
  };

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

export default App;

I expected the paragraph to update and display the new count each time I clicked the button, but it remains at 0. I've verified that the handleClick function is being called and setCount is executing.
reactjs state hook rerender
1个回答
0
投票

您需要将其用作回调函数。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.