【问题标题】:How do you call a parent function from a child component (with parameters) in react-native?如何在 react-native 中从子组件(带参数)调用父函数?
【发布时间】:2020-02-27 10:03:59
【问题描述】:

我有一个名为 HomeHeader 的反应组件。 HomeHeader 中有一个名为 SearchResultFlatList 的无状态组件。在 SearchResultFlatList 中有一个称为 SearchListItem 的无状态组件。 HomeHeader 有 2 个输入字段(一个用于位置,一个用于目的地)。在输入字段中输入后,SearchResultFlatList 会被创建并重新填充 SearchListItems(每次我输入内容(想想谷歌搜索))。单击 SearchListItem 时,我希望能够调用驻留在 HomeHeader 上的函数。要实现的功能是单击 SearchListItem,并在 HomeHeader 上填充相应的输入字段。

我在 HomeHeader 上声明了一个名为 onLocationPress 的函数。它采用一个参数(即 onLocationPress(locationValue))。将此函数的引用传递给子组件非常简单,但涉及变量时就变得更加困难。

来自 HomeHeader.js

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

来自 SearchResultFlatList.js

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

来自 SearchListItem.js

const SearchListItem = (props) => {

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

以这种方式调用它会导致函数被调用很多次。我在输入字段上还有一个onTextChanged() 函数,每次我输入它都会记录props.primaryText 值。

我认为这与我每次键入时都会创建新的 SearchListItems 的事实有关,但我不知道如何解决。

【问题讨论】:

    标签: javascript reactjs react-native parameter-passing parent-child


    【解决方案1】:

    您不能通过参数将函数传递给 onPress。而是定义一个箭头函数来执行您的 clickLocationResult 参数,如下所示。

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

    【讨论】:

    • 是的 - 结果和我的一样,而且方法干净。
    • 问题:@Benjamin,如果没有参数,只使用我做的方式就足够了吗? (例如 onPress={props.clickLocationResult})????此实现是否特定于使用变量?如果是,请解释原因
    • @JakeChambers 是的,完全正确!这是因为 onPress 接受一个函数作为参数。 onPress={myFunction} 如果您在 myFunction 之后添加括号,则在解释器评估代码时调用该函数,然后将其返回值提供给 onPress,这不是我们想要的。所以想象一下,如果我们这样定义我们的函数:const myFunction = () =&gt; 'hello'; 调用 myFunction() 返回 'hello'。
    • @JakeChambers ...继续。那么如果我们在 onPress 中这样调用它:onPress={myFunction()}。解释器出现,并执行 myFunction,它返回 'hello',所以现在 onPress 接收 'hello' 作为它的参数:onPress={'hello'},当按下事件被触发时,你会得到一个错误,因为它期望接收一个函数, 而是收到一个字符串。
    【解决方案2】:

    你能在 HomeHeader 中发布你的 onLocationPress 函数吗?

    我怀疑你需要的是一种叫做函数柯里化的东西。有很多关于这方面的资源,但基本思想是您的函数将被定义为:

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

    当您向 onPress 处理程序提供带有参数的函数时,该函数将被执行并且结果将返回到处理程序,而不是函数本身。你想要的是让 onLocationPress 成为一个返回另一个函数的函数。

    【讨论】:

    • 问题:@Jaked222 如果没有参数,使用我的方法就足够了吗? (例如 onPress={props.clickLocationResult})????此实现是否特定于使用变量?
    • 见本杰明的评论。 onPress 期待一个功能。给它“props.clickLocationResult”是一个函数。 'props.clickLocationResult(example)' 是 clickLocationResult RETURNS。 javascript函数中的好东西可以返回其他函数。
    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 2016-09-07
    • 2018-03-28
    • 2020-11-02
    • 1970-01-01
    • 2017-04-15
    • 2020-04-23
    • 1970-01-01
    相关资源
    最近更新 更多