Javascript 将本机传递函数作为道具反应到子组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49547751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:23:54  来源:igfitidea点击:

React Native pass function to child component as prop

javascriptreactjsreact-native

提问by Jerred Shepherd

I'm new to React Native (and React), and I'm trying to pass a function as a prop to a component.

我是 React Native(和 React)的新手,我正在尝试将函数作为 prop 传递给组件。

My goal is to create a component where its onPress functionality can be set by the instantiator of the component, so that it is more reusable.

我的目标是创建一个组件,它的 onPress 功能可以由组件的实例化器设置,以便更可重用。

Here is my code so far.

到目前为止,这是我的代码。

App.js

应用程序.js

import React, { Component } from 'react';
import { View } from 'react-native';
import TouchableButton from './components/touchable-button';

export default class App extends Component<Props> {
  constructor () {
    super();
  }

  handlePress () {
    // this should be called when my custom component is clicked
  }

  render () {
    return (
      <View>
        <TouchableButton handlePress={this.handlePress.bind(this)}/>
      </View>
    );
  }
}

TouchableButton.js

TouchableButton.js

import React, { Component } from 'react';
import { TouchableHighlight } from 'react-native';
import AppButton from "./app-button";

export default class TouchableButton extends Component {
  handlePress;

  constructor(props){
    super(props);
  }

  render () {
    return (
      <TouchableHighlight onPress={
        this.props.handlePress
      }>
        <AppButton/>
      </TouchableHighlight>
    );
  }
}

I am passing the handlePress function as the prop handlePress. I would expect the TouchableButton's props to contain that function, however it isn't there.

我将 handlePress 函数作为道具 handlePress 传递。我希望 TouchableButton 的道具包含该功能,但它不存在。

采纳答案by Moti Korets

When writing handlePress={this.handlePress.bind(this)}you passing a statement execution ( which when and if executed returns a function). What is expected is to pass the function itself either with handlePress={this.handlePress}(and do the binding in the constructor) or handlePress={() => this.handlePress()}which passes an anonymous function which when executed will execute handlePress in thisclass context.

在编写handlePress={this.handlePress.bind(this)}时传递一个语句执行(当和如果执行时返回一个函数)。期望的是传递函数本身handlePress={this.handlePress}(并在构造函数中进行绑定)或handlePress={() => this.handlePress()}传递匿名函数,该函数在执行时将在this类上下文中执行 handlePress 。

回答by Jeff Gu Kang

Solution

解决方案

Use arrow functionfor no care about binding this.

使用箭头函数而不关心绑定this

And I recommend to check null before calling the props method.

我建议在调用 props 方法之前检查 null。

App.js

应用程序.js

export default class App extends Component<Props> {
  constructor () {
    super();
  }

  handlePress = () => {
    // Do what you want. 
  }

  render () {
    return (
      <View>
        <TouchableButton onPress={this.handlePress}/>
      </View>
    );
  }
}

TouchableButton.js

TouchableButton.js

import React, { Component } from 'react';
import { TouchableHighlight } from 'react-native';
import AppButton from "./app-button";

export default class TouchableButton extends Component {
  constructor(props){
    super(props);
  }
  
  handlePress = () => {
    // Need to check to prevent null exception. 
    this.props.onPress?.(); // Same as this.props.onPress && this.props.onPress();
  }

  render () {
    return (
      <TouchableHighlight onPress={this.handlePress}>
        <AppButton/>
      </TouchableHighlight>
    );
  }
}

回答by Naycho334

// Parent

handleClick( name ){
   alert(name);
}

<Child func={this.handleClick.bind(this)} />

// Children

let { func } = this.props;

func( 'VARIABLE' );