【发布时间】:2022-12-01 01:03:33
【问题描述】:
我在用使用备忘录功能组件中的反应钩子。 我不确定为什么控制台日志被打印两次。下面是我的代码:
import './App.css';
import react,{useState,useMemo} from 'react';
function App() {
const [count,setCount] = useState(0);
const [item,setItem] = useState(10);
const multiCountMemo = useMemo(function multiCount() {
console.log("to check if getting inside the function") <---- this is getting printed twice by default on load app page.
return count * 5
},[count])
return (
<div className="App">
<h1>useMemo Hook Usage</h1>
<h2>Count : {count}</h2>
<h2>Item : {item}</h2>
<h2>{multiCountMemo}</h2>
<button onClick={() => setCount(count + 1)}>Update Count</button>
<button onClick={() => setItem(item * 10)}>Update Item</button>
</div>
);
}
export default App;
【问题讨论】:
-
这回答了你的问题了吗? Why my render method is react called twice
-
你在哪个反应版本?
-
我正在使用“反应”:“^ 18.1.0”,
标签: javascript reactjs react-hooks