简体   繁体   English

React Navigation Header 按钮(V5 / 版本 5.x):显示菜单项

[英]React Navigation Header Button (V5 / Version 5.x): Show Menu Item

I am using React-Native-Paper's Menu Component我正在使用React-Native-Paper 的菜单组件

I am trying to show Menu item from the header after tapping an icon.点击图标后,我试图显示 header 中的菜单项。

So far I have managed to show list of item on the screen only.到目前为止,我已经设法仅在屏幕上显示项目列表。

However, I want to show the menu list from the header.但是,我想显示 header 的菜单列表。

Due to React Navigation's update from version:4.x to version:5.x, I am a bit confused on how to work this out.由于 React Navigation 从 version:4.x 更新到 version:5.x,我对如何解决这个问题有点困惑。 I tried following the example here but still I need some time to fully understand hook and the way it works.我尝试按照此处的示例进行操作,但仍然需要一些时间来完全理解钩子及其工作方式。

All kinds of help would be appreciated.各种帮助将不胜感激。

Workable/Testable code link, code snippets and screenshots provided below:下面提供的可行/可测试代码链接、代码片段和屏幕截图:

Snack Link小吃链接

Register.js:注册.js:

import { TextInput, Button, Menu, Divider, Provider } from 'react-native-paper';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';

class Register extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
    }
  }

  //[ TEST DROP DOWN MENU ]
  _openMenu = () => this.setState({ visible: true });

  _closeMenu = () => this.setState({ visible: false });

  renderMenuExample() {
    return (
      <View
        style={{
          paddingTop: 50,
          flexDirection: 'row',
          justifyContent: 'center'
        }}>
        <Menu
          visible={this.state.visible}
          onDismiss={this._closeMenu}
          anchor={
            < TouchableOpacity
              onPress={this._openMenu}
              style={{ marginHorizontal: 20 }}
            >
              <MaterialCommunityIcons name="earth" size={30} style={{ color: 'black' }} />
            </TouchableOpacity>
          }
        >
          <Menu.Item onPress={() => { }} title="Item 1" />
          <Menu.Item onPress={() => { }} title="Item 2" />
          <Divider />
          <Menu.Item onPress={() => { }} title="Item 3" />
        </Menu>
      </View>
    );
  }
  //[ TEST DROP DOWN MENU ]

  render() {
    return (
      <Provider>
        {this.renderMenuExample()}
      </Provider>
    );
  }
}

export default Register;

App.js:应用程序.js:

import { TextInput, Button, Menu, Divider, Provider } from 'react-native-paper';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import AntDesign from 'react-native-vector-icons/AntDesign';

const Stack = createStackNavigator();

function App() {
  return (
    <NavigationContainer>
      <Stack.Navigator>
        <Stack.Screen
          name="Register"
          component={Register}
          options={{
            headerBackImage: () => (
              <AntDesign name="close" size={30} style={{ color: 'white' }} />
            ),
            headerTitle: () => (
              <View>
                <Text style={{ flex: 1, fontSize: 20, fontWeight: 'bold', alignSelf: 'center', color:                   'white' }}>
                  Register
                </Text>
              </View>
            ),
            headerRight: () => (
              <TouchableOpacity
                onPress={() => {/* I WANT TO SHOW THE MENU HERE */ }}
                style={{ marginHorizontal: 20 }}
              >
                <MaterialCommunityIcons name="earth" size={30} style={{ color: 'white' }} />
              </TouchableOpacity>
            ),
            headerStyle: {
              backgroundColor: '#2e46ff',
            },
          }}
        />
      </Stack.Navigator>
    </NavigationContainer>
  );
}

export default App;

Screenshot :截图

在此处输入图像描述

One possible solution would be to wrap the navigation container with a provider and have a separate component for the menu.一种可能的解决方案是使用提供程序包装导航容器,并为菜单提供单独的组件。 Tried this with your snack and it works用你的零食试过这个,它有效

The code would be as below.代码如下。

const CustomMenu = () => {
  const [showMenu, setShowMenu] = React.useState(false);

  return (
    <View style={{}}>
      <Menu
        visible={showMenu}
        onDismiss={() => setShowMenu(false)}
        anchor={
          <TouchableOpacity onPress={() => setShowMenu(true)}>
            <MaterialCommunityIcons
              name="earth"
              size={30}
              style={{ color: 'black' }}
            />
          </TouchableOpacity>
        }>
        <Menu.Item onPress={() => {}} title="Item 1" />
        <Menu.Item onPress={() => {}} title="Item 2" />
        <Divider />
        <Menu.Item onPress={() => {}} title="Item 3" />
      </Menu>
    </View>
  );
};

function App() {
  return (
    <Provider>
      <NavigationContainer>
        <Stack.Navigator>
          <Stack.Screen
            name="Register"
            component={Register}
            options={{
              headerBackImage: () => (
                <AntDesign name="close" size={30} style={{ color: 'white' }} />
              ),
              headerTitle: () => (
                <View>
                  <Text
                    style={{
                      flex: 1,
                      fontSize: 20,
                      fontWeight: 'bold',
                      alignSelf: 'center',
                      color: 'white',
                    }}>
                    Register
                  </Text>
                </View>
              ),
              headerRight: () => <CustomMenu />,
              headerStyle: {
                backgroundColor: '#2e46ff',
              },
            }}
          />
        </Stack.Navigator>
      </NavigationContainer>
    </Provider>
  );
}

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

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