简体   繁体   English

设置 state 时使用 React Hook 时的警告 useCallback

[英]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.

相关问题 何时使用 React useCallback 钩子 - When to use React useCallback hook 调用时 React Hook useCallback 未更新 state - React Hook useCallback not getting updated state when invoked 在 useCallback() 钩子中反应 setState 没有正确设置状态变量? - React setState inside useCallback() hook not setting state variable correctly? 使用钩子设置 React 状态时未定义 - React state is undefined when setting it with a hook 使用 React.memo 和 useCallback 时 State 未正确更新 - State is not updated properly when using React.memo and useCallback 在 useCallback 挂钩中使用 Axios 时反应功能组件无限重新渲染? - React functional component re-rendering infinitely when using Axios in useCallback hook? React Hook useCallback 不更新状态值 - React Hook useCallback not updating State value 在从 `useEffect` 调用的`useCallback` 中设置和使用 state 时的无限循环 - Infinite loop when setting and using state in a `useCallback` that is being called from a `useEffect` 当 useCallback 缺少依赖时 React useState 和 useCallback hook 如何工作 - How does React useState and useCallback hook work when useCallback lacks dependencies 最佳实践:设置加载状态时响应 useEffect 钩子依赖项 - Best practice: React useEffect hook dependencies when setting loading state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM