简体   繁体   English

React/React Native viewableItems 解构问题

[英]React/React Native viewableItems destructuring question

This is React/React Native but the question I feel is related to JavaScript. Anyway, this is what I have:这是 React/React Native 但我觉得问题与 JavaScript 有关。无论如何,这就是我所拥有的:

onViewableItemsChanged={({ viewableItems }) => {
    if (viewableItems.length > 0) {
        setActiveIndex(viewableItems[0].index || 0)
    }
}}

Here, viewableItems is something that is destructured.在这里, viewableItems是被解构的东西。 We can access it because we are working with onViewableItemsChanged .我们可以访问它,因为我们正在使用onViewableItemsChanged But when I move this function elsewhere, such as:但是当我把这个 function 移到别处时,比如:

onViewableItemsChanged={onFlatlistUpdate}
const onFlatlistUpdate = useCallback(({ viewableItems }) => {
    if (viewableItems.length > 0) {
        setActiveIndex(viewableItems[0].index || 0)
    }
}, [])

I dont get where viewableItems is from?我不知道viewableItems是从哪里来的? I mean in the first example, it is destructured from the context of onViewableItemsChanged .我的意思是在第一个示例中,它是从onViewableItemsChanged的上下文中解构的。 However in the second example, it is completely independent, away from onViewableItemsChanged (of course we are using useCallback but it does not matter) so how come we have access to it?然而在第二个例子中,它是完全独立的,远离onViewableItemsChanged (当然我们使用的是useCallback但没关系)那么我们怎么能访问它呢?

TL DR: In the 2nd example (onFlatlistUpdate), where is viewableItems destructured from/how come we have access to viewableItems ? TL DR:在第二个示例(onFlatlistUpdate)中, viewableItems在哪里被解构/我们如何访问viewableItems

In both examples, you're passing a function to the onViewableItemsChanged prop.在这两个示例中,您都将 function 传递给onViewableItemsChanged道具。 In the first case, you're creating an anonymous function inline, and that function reference is passed to onViewableItemsChanged .在第一种情况下,您正在创建一个匿名的 function 内联,并将该 function 引用传递给onViewableItemsChanged In your second case, you're creating a function with useCallback() which you're then passing to onViewableItemsChanged (the function returned by useCallback() will forward any of the arguments it's passed to the function you give it).在第二种情况下,您正在使用useCallback()创建一个 function,然后将其传递给onViewableItemsChangeduseCallback()返回的 function 会将传递给它的任何 arguments 转发给您提供的 function)。 Internally, the FlatList component with the onViewableItemsChanged prop will call that function and pass an object with viewableItems , such as onViewableItemsChanged({viewableItems: ...}) .在内部,带有onViewableItemsChangedFlatList组件将调用 function 并传递带有viewableItems的 object,例如onViewableItemsChanged({viewableItems: ...}) This allows you to destructure the object within the function you've passed to your FlatList component.这允许您在传递给FlatList组件的 function 中解构 object。

I mean in the first example, it is destructured from the context of onViewableItemsChanged我的意思是在第一个示例中,它是从 onViewableItemsChanged 的上下文中解构的

onViewableItemsChanged doesn't really have a "context". onViewableItemsChanged并没有真正的“上下文”。 onViewableItemsChanged is just a prop that you pass a function to. onViewableItemsChanged只是您将 function 传递给的道具。 How that function is then called within FlatList determines the arguments of the function. That's handled by the internals of FlatList component which you don't need to worry much about.然后在 FlatList 中调用FlatList的方式决定了 function 的 arguments。这由FlatList组件的内部处理,您无需太担心。 You can create that function independently, or create it inline, in both cases, you're passing a function reference to onViewableItemsChanged that can be called by FlatList .您可以独立创建 function ,也可以内联创建它,在这两种情况下,您都将 function 引用传递给可以由onViewableItemsChanged调用的FlatList

Take the following example code using vanilla JS, as we pass a callback function to another function for it to be invoked.以使用 vanilla JS 的以下示例代码为例,当我们将回调 function 传递给另一个 function 以调用它时。 It doesn't matter if we define the function as standalone or inline, all the really matters is that we end up passing a function to myFlatList :我们将 function 定义为独立的还是内联的并不重要,真正重要的是我们最终将 function 传递给myFlatList

 const myFlatList = (onViewableItemsChanged) => { onViewableItemsChanged({viewableItems: [1, 2, 3]}); } // Defining the callback function inline myFlatList(({viewableItems}) => { console.log("inline", viewableItems); }); // Similar to your `useCallback()` definition const callbackFn = ({viewableItems}) => { console.log("independent", viewableItems); } myFlatList(callbackFn);

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

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