简体   繁体   English

在这个例子中使用 React useCallback 是否合乎逻辑?

[英]Is logical to use React useCallback for this example?

I'm new in React hooks, in one of parts in our projects, we have a function to handle modal display in cards:我是 React 钩子的新手,在我们项目的一个部分中,我们有一个 function 来处理卡片中的模式显示:

 const toggleModal = () => setModalDisplay(!modalDisplay)

in this case, I have to pass this function to each child components... maybe for 10 or 1000 children!在这种情况下,我必须将此 function 传递给每个子组件……也许是 10 或 1000 个孩子!

So, regard to React JS DOC useCallback returns a memoized function to reduce the memory usage and handle expensive computing.因此,关于 React JS DOC useCallback返回一个记忆化的 function 以减少 memory 的使用并处理昂贵的计算。 but I have to pass params to second argument to refresh callback and return the new one.但我必须将参数传递给第二个参数以刷新回调并返回新的。

but in this case, we have a state to handle modal toggle, that every times has changes between true and false.但在这种情况下,我们有一个 state 来处理模态切换,每次都会在真假之间变化。 so, with considering the logic of useCallback and our use case, if I use useCallback for this example, practically that function ( toggleModal ) updates every times and useCallback is useless for this use case.所以,考虑到useCallback的逻辑和我们的用例,如果我在这个例子中使用useCallback ,实际上 function ( toggleModal ) 每次都会更新,而useCallback对于这个用例是没用的。

for example:例如:

 const memoizedModalToggle = useCallback( () => { setModalDisplay(,modalDisplay) }, [modalDisplay]; );

finally, is this a correct using of useCallback ?最后,这是正确使用useCallback吗? or I have mistake in implementation and logic?还是我在实现和逻辑上有错误?

Thanks谢谢

I think you are confused between set the states and update callback functions in a memoized function with useCallback我认为您在使用 useCallback 的记忆化useCallback中设置状态和更新回调函数之间感到困惑

For example, you can memoized your callback function with an empty array of dependencies:例如,您可以使用空的依赖项数组来记忆您的回调 function:

 const memoizedModalToggle = useCallback( () => { setModalDisplay(,modalDisplay) }; [] );

it will be something like useEffect logic for callback functions.它将类似于回调函数的useEffect逻辑。

Also you can use Set() object to test this flow, because Set is able to store unique functions by references and you are able to use set.size() for checking the number of callbacks have generated.您也可以使用Set() object 来测试此流程,因为Set能够通过引用存储独特的函数,并且您可以使用set.size()来检查已生成的回调数量。

ok, use useCallback when you want to avoid having to update a component or function when the state changes好的,当您想避免更新组件时使用 useCallback 或在 state 更改时使用 function

example:例子:

    {useCallback(<TableStore
        params={[JSON.stringify([selectedRoute]), data]} 
        tableName="custom_table" 
        method="Select"
    >
        <ReactTable
            allowExport
            onRowClick={onRowClick}
            columns={columns}
            allowSelection={false}
        />
    </TableStore>, [selectedRoute, data])}

Here the table will change and update only when we click on the row or when our data has been updated.只有当我们单击行或我们的数据已更新时,表才会更改和更新。

in your case, not need to use useCallback在你的情况下,不需要使用 useCallback

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM