简体   繁体   English

如何在反应导航中连接反应组件?

[英]How to connect react component in react navigation?

I have created modal in react native. 我已经创建了modal in native native。 I have added filter option icon in top right corner now when the user clicks on it should open the modal. 我现在在右上角添加了过滤器选项图标,当用户点击它时应打开模态。 How can I do that ? 我怎样才能做到这一点 ? I have added Options icon in navigation.js but now how to connect it with modal component ? 我在navigation.js中添加了选项图标,但现在如何将其与模态组件连接?

In code below setModalVisible is available in filteroptions.js and not in navigation.js 在下面的代码中, setModalVisiblesetModalVisible中可用,而不在navigation.js中

Code: 码:

navigation.js: navigation.js:

Updates: {
            screen: UpdatesScreen,

            navigationOptions: ({ navigation }) => ({
                headerTitle: 'Myapp',
                headerRight:<TouchableOpacity onPress={() => {this.setModalVisible(true);}}>
                                <MenuIcon style={{paddingLeft: 10, paddingRight: 15}} name="md-options" size={25} color="white"/>
                            </TouchableOpacity>,

            })
        }, 

filteroptions.js: filteroptions.js:

import React, {Component} from 'react';
import {Modal, Text, TouchableHighlight, View, Alert} from 'react-native';

export default class FilteroptionsModel extends Component {
  state = {
    modalVisible: false,
  };

  setModalVisible(visible) {
    this.setState({modalVisible: visible});
  }

  render() {
    return (
      <View style={{marginTop: 22}}>
        <Modal
          animationType="slide"
          transparent={false}
          visible={this.state.modalVisible}
          onRequestClose={() => {
            Alert.alert('Modal has been closed.');
          }}>
          <View style={{marginTop: 22}}>
            <View>

              <TouchableHighlight
                onPress={() => {
                  this.setModalVisible(!this.state.modalVisible);
                }}>
                <Text>Hide Modal</Text>
              </TouchableHighlight>
            </View>
          </View>
        </Modal>
      </View>
    );
  }
}

When user clicks on filter button in top right corner (see screenshot) he should be able to see the box modal on screen: 当用户点击右上角的过滤器按钮时(见截图),他应该能够在屏幕上看到框模式:

在此输入图像描述

Since it is a modal you don't necessarily need to add it to the navigation. 由于它是模态,因此您不一定需要将其添加到导航中。 You can just include it in your page and initially have it be invisible, then when the user clicks your button you can make the modal visible. 您可以将其包含在您的页面中并最初使其不可见,然后当用户单击您的按钮时,您可以使模式可见。 But if you want to add it to the navigation why not just add like any other component. 但是如果你想将它添加到导航中,为什么不像其他任何组件一样添加它。 However, adding it to the navigation will take you to another page when you navigate to the component. 但是,将其添加到导航将导航到导航到组件时转到另一个页面。 Of course you can add a nested navigator to work around that, but I think it adds unnecessary complexity. 当然,您可以添加嵌套导航器来解决这个问题,但我认为这会增加不必要的复杂性。

Update You will first declare a Header component. 更新您将首先声明一个Header组件。

export default class MyHeader extends React.PureComponent {

  render() {
    <View>
      <View>...Your left Icon here</View>
      <View>...Your Title here</View>
      <View>...Your right Icon Here</View>
    </View>
  }
}

Then in your page you will render this component first, pass as props your handlers and then render the rest of the page. 然后在您的页面中,您将首先渲染此组件,将您的处理程序作为道具传递,然后渲染页面的其余部分。

export default class MyPage extends React.PureComponent {

  yourRigthHandler = () => {
     this.setState({modaVisible: true});
  }

  yourLeftHandler = () => {....}

  render() {
    <View>
      <MyHeader 
         LeftHandler={yourLeftHandler}
         LeftHandler={yourRigthHandler}>
           ....
      </MyHeader>
    </View>
  }
}

inside the handlers you can call the navigation functions to navigate to another page or change the components state to make the moda visible.The handlers will be passed as props to your header and you will add them as onPress handlers to your buttons. 在处理程序内部,您可以调用导航功能导航到另一个页面或更改组件状态以使模型可见。处理程序将作为道具传递到您的标题,您将它们作为onPress处理程序添加到您的按钮。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM