react-native - 在 React Native 中,如何从另一个组件访问一个组件的方法?

标签 react-native

我正在尝试从不同的组件访问 React Native 组件的方法。它是通过 props 传递的。不幸的是,这些组件似乎没有公开提供它们的方法。我如何才能访问该方法?

看看下面,你会看到 InsideView 有 this.props.myModal,它是一个 ShowMyModal 组件。但是,它无权访问 .openModal() 方法。

enter image description here

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  ActionSheetIOS,
  StyleSheet,
  Text,
  View,
} = React;

var InsideView = React.createClass({
  makeItOpen: function() {
    debugger;
    this.props.myModal.openModal();
  },

  render: function() {
    return (
      <View>
        <Text onPress={() => this.makeItOpen()}>Click me!</Text>
      </View>
    );
  }
});

var ShowMyModal = React.createClass({
  getInitialState: function() {
    return {
      isModalOpen: false,
    }
  },

  openModal() {
    this.setState({isModalOpen: true});
  },

  closeModal() {
    this.setState({isModalOpen: false});
  },

  render: function() {
    return (
      <Text>isModalOpen = {String(this.state.isModalOpen)}</Text>
    );
  }
});

var AwesomeProject = React.createClass({
  getInitialState: function() {
    return {
      myModal: <ShowMyModal />,
    }
  },

  render: function() {
    return (
      <View style={{padding: 30}}>
        <InsideView myModal={this.state.myModal}/>
        {this.state.myModal}
      </View>
    );
  },
});

AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);

最佳答案

这样的事情应该工作:

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  ActionSheetIOS,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} = React;

var InsideView = React.createClass({
  render: function() {
    return (
      <View>
        <TouchableOpacity onPress={() => this.props.openModal()}><Text>Open modal!</Text></TouchableOpacity>
        <TouchableOpacity onPress={() => this.props.closeModal()}><Text>Close modal!</Text></TouchableOpacity>
      </View>
    );
  }
});

var ShowMyModal = React.createClass({
  render: function() {
    return (
      <Text>isModalOpen = {String(this.props.isVisible)}</Text>
    );
  }
});

var SampleApp = React.createClass({
  getInitialState: function() {
    return {
      isModalOpen: false
    }
  },

  _openModal: function() {
    this.setState({
      isModalOpen: true
    });
  },

  _closeModal() {
    this.setState({
      isModalOpen: false
    });
  },

  render: function() {
    return (
      <View style={{padding: 30}}>
        <InsideView openModal={this._openModal} closeModal={this._closeModal}/>
        <ShowMyModal isVisible={this.state.isModalOpen}/>
      </View>
    );
  },
});

AppRegistry.registerComponent('SampleApp', () => SampleApp);

关于react-native - 在 React Native 中,如何从另一个组件访问一个组件的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31998091/

相关文章:

android - 找不到com.android.tools.build:gradle:3.6.2

reactjs - React Native 设置状态不起作用

reactjs - 未编译的 PNG 文件作为参数传递。必须首先编译成 .flat 文件..错误 : failed parsing overlays

react native react native 矢量图标 : How to use font awesome icons

ios - 为 testflight、应用程序图标存档 react native iOS 项目时出错

javascript - 我如何在 React Native 中定义一个枚举

react-native - React Native Android - 为什么无法与父组件边界之外的绝对定位 View 进行交互?

android - 如何在 React Native 中使用来自服务器的 json 数据动态显示底部选项卡导航?

javascript - 使用 firebase 身份验证和 firestore 添加用户

React-native-navigation 从另一个 tabnavigator 更改状态