[英]warning when using React Hook useCallback when setting state
I have a website with a super simple navigation.我有一个导航超级简单的网站。
It just displays different components based on the boolean value of a state property.它仅根据 state 属性的 boolean 值显示不同的组件。
It's working fine, but I have this nagging warning that shows on the functions that handle clicks.它工作正常,但我有这个烦人的警告,显示在处理点击的函数上。
The warning is here:警告在这里:
useCallback does nothing when called with only one argument.
But I'm not sure why it's telling me that because my navigation is working.但我不知道为什么它告诉我,因为我的导航正在工作。
Do I need to add something else to make it work better?我是否需要添加其他内容以使其更好地工作?
Thanks!谢谢!
Here is my little component for navigation.这是我的导航小组件。
// creat state
const [showCreationPage, setCreationPage] = useState(true);
const [showDisplayPage, setDisplayPage] = useState(false);
// here is warning sign: useCallback does nothing when called with only one argument.
const handleCreationPage = useCallback(e => {setDisplayPage(false) || setCreationPage(true) });
const handleDisplayPage = useCallback(e => { setCreationPage(false) || setDisplayPage(true) });
// navigation buttons
<a href="#" onClick={handleCreationPage}>Create Beer</a>
<a href="#" onClick={handleDisplayPage}>Display Beer</a>
<div id="main">
<div>
{showCreationPage && <Create />}
</div>
<div>
{showDisplayPage && <Display />}
</div>
</div>
useCallback
expects an array of dependencies as a second argument. useCallback
需要一个依赖数组作为第二个参数。 That tells the memoization to update whenever the value of one of the dependencies is updated.每当更新依赖项之一的值时,这就会告诉 memoization 进行更新。 If you never want the callback function to update, just pass an empty array.
如果您不希望回调 function 更新,只需传递一个空数组。
Reference: https://reactjs.org/docs/hooks-reference.html#usecallback参考: https://reactjs.org/docs/hooks-reference.html#usecallback
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.