简体   繁体   English

如何从 react-native 中的子组件(带参数)调用父 function?

[英]How do you call a parent function from a child component (with parameters) in react-native?

I have a react component called HomeHeader.我有一个名为 HomeHeader 的反应组件。 Within HomeHeader lies a stateless component called SearchResultFlatList. HomeHeader 中有一个名为 SearchResultFlatList 的无状态组件。 Within SearchResultFlatList lies a stateless component called SearchListItem.在 SearchResultFlatList 中有一个称为 SearchListItem 的无状态组件。 HomeHeader has 2 input fields (one for location and one for destination). HomeHeader 有 2 个输入字段(一个用于位置,一个用于目的地)。 Upon typing in an input field, SearchResultFlatList gets created and re-populated with SearchListItems (everytime I type something (think google search)) .在输入字段中输入后,SearchResultFlatList 会被创建并重新填充 SearchListItems(每次我输入一些东西(想想谷歌搜索)) When a SearchListItem is clicked, I would like to be able to call a function residing on HomeHeader.单击 SearchListItem 时,我希望能够调用驻留在 HomeHeader 上的 function。 The functionality to be achieved is clicking a SearchListItem, and populating the respective input field on HomeHeader.要实现的功能是单击 SearchListItem,并在 HomeHeader 上填充相应的输入字段。

I have declared a function on HomeHeader called onLocationPress.我已经在名为 onLocationPress 的 HomeHeader 上声明了一个 function。 It takes on one argument (ie. onLocationPress(locationValue)).它采用一个参数(即 onLocationPress(locationValue))。 It is very simple to pass down the reference to this function to the child components, but becomes more difficult when a variable is involved.将这个 function 的引用传递给子组件非常简单,但在涉及变量时变得更加困难。

From HomeHeader.js来自 HomeHeader.js

<SearchResultFlatList
   containerOpacity={this.state.opacityProp}
   headerExpanded={this.state.headerExpanded}
   results={this.props.searchResults}
   clickLocationResult={this.onLocationPress}
/>

From SearchResultFlatList.js来自 SearchResultFlatList.js

const SearchResultFlatList = (props) => {
    return(
        <Animated.View>
        <FlatList
          ....
            <SearchListItem
                clickLocationResult={props.clickLocationResult}
                primaryText={item.primaryText}
                fullText={item.fullText}
            />)}
          ....
        />
    </Animated.View>
    )
}

From SearchListItem.js来自 SearchListItem.js

const SearchListItem = (props) => {

    return (
        <TouchableOpacity 
            style={styles.searchListItemContainer}
            onPress={props.clickLocationResult(props.primaryText)}
        >
        ....
        </TouchableOpacity>
    );
  }

Calling it this way results in the function getting called soooo many times.以这种方式调用它会导致 function 被多次调用。 I also have an onTextChanged() function on the input field, and everytime I type it logs the props.primaryText value.我在输入字段上还有一个onTextChanged() function,每次我输入它都会记录props.primaryText值。

I'm thinking it has something to do with the fact that everytime I type, new SearchListItems are getting created, but I don't know the work around.我认为这与每次键入时都会创建新的 SearchListItems 的事实有关,但我不知道解决方法。

You can't pass a function to onPress with parameters.您不能通过参数将 function 传递给 onPress。 Instead define an arrow function that executes your clickLocationResult with the parameter like so.而是定义一个箭头 function 执行 clickLocationResult ,参数如下。

const SearchListItem = props => {
  return (
    <TouchableOpacity
      style={styles.searchListItemContainer}
      onPress={() => props.clickLocationResult(props.primaryText)}
    >
      ....
    </TouchableOpacity>
  );
};

Can you post what your function onLocationPress looks like in HomeHeader?您可以在 HomeHeader 中发布您的 function onLocationPress 的样子吗?

I am suspecting that what you need is something called function currying.我怀疑你需要的是一种叫做 function 柯里化的东西。 There are a lot of resources out there on this, but the basic idea is that your function would be defined like this:这方面有很多资源,但基本思想是您的 function 将定义如下:

function onLocationPress(text) {
    // this is your click handler
    return () => {
        // do something with that text
    }
}

When you supply the onPress handler with a function with a parameter, that function is being EXECUTED and the result is being returned to the handler, as opposed to the function itself.当您为 onPress 处理程序提供带有参数的 function 时,function 正在执行,结果将返回到处理程序,而不是 ZC1C425268E68385D1AB5074F17A 本身。 What you want is for onLocationPress to be a function which returns another function .您想要的是 onLocationPress 成为 function ,它返回另一个 function

暂无
暂无

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

相关问题 React Native - 如何从父组件调用子组件的 function(不触发无限循环)? - React Native - how do you call the function of a child component from its parent (without triggering an infinite loop)? 如何从孩子呼叫父母 function - react-native - How to call a parent function from a child - react-native React Native中如何从子组件调用父函数并将子组件的参数传递给父组件? - How can calling parent function from child component and passing parameters from child component to parent component in React Native? 通过函数参数将属性从动态子组件传递到父功能组件 react-native - Passing properties from a dynamic Child component to a Parent functional component through a functions parameters react-native react-Native:在父组件内部访问子组件函数 - react-Native: Access child component function inside parent component 如何从父组件调用 React/Typescript 子组件 function? - How to call React/Typescript child component function from parent component? React-Native:从父组件调用组件内的 function - React-Native: Calling a function within a component from parent component in 如何从 React 中的父组件调用子 function - How can I call Child function from Parent Component in React React Native-如何从子组件中调用函数? - React Native - How to call a function from a child component? 如何在 React Native 中调用子组件 function? - How to call child component function in React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM