相关疑难解决方法(0)

将本机传递函数作为prop反映到子组件

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

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

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

App.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>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

TouchableButton.js

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

export …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-native

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

javascript ×1

react-native ×1

reactjs ×1