Home > Net >  I need to get option when click the text using TouchableOpacity in React Native
I need to get option when click the text using TouchableOpacity in React Native

Time:10-14

I got below code can convert into a functional component or give me another code

React Native on click show options

CodePudding user response:

Code:

Might be helpful:

import React from 'react';
import {View, Text} from 'react-native';
import Menu, {MenuItem, MenuDivider} from 'react-native-material-menu';

export default App = () => {
  const _menu = React.useRef();

  const hideMenu = () => {
    _menu.current.hide();
  };

  const showMenu = () => {
    _menu.current.show();
  };

  return (
    <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
      <Menu
        ref={_menu}
        button={<Text onPress={() => showMenu()}>Show menu</Text>}>
        <MenuItem onPress={() => hideMenu()}>Menu item 1</MenuItem>
        <MenuItem onPress={() => hideMenu()}>Menu item 2</MenuItem>
        <MenuItem onPress={() => hideMenu()} disabled>
          Menu item 3
        </MenuItem>
        <MenuDivider />
        <MenuItem onPress={() => hideMenu()}>Menu item 4</MenuItem>
      </Menu>
    </View>
  );
};

CodePudding user response:

Below is the full code using function that you want. It might be working for you.

import React, { useState } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { Menu, MenuItem, MenuDivider } from 'react-native-material-menu';

export default function App() {
  const [state, setState] = useState({ text: 'Show' });

  const [visible, setVisible] = useState(false);

  const hideMenu = () => {
    setVisible(false), 
    setState({ text: 'Show' });
  };

  const showMenu = () => {
    setVisible(true), 
    setState({ text: 'Hide' });
  };

  return (
    <View
      style={{
        height: '100%',
        alignItems: 'center',
        justifyContent: 'center',
      }}>
      <TouchableOpacity onPress={showMenu}>
        <Text>{state.text}</Text>
      </TouchableOpacity>
      <Menu
        visible={visible}
        // anchor={<Text onPress={showMenu}>Menu</Text>}
        onRequestClose={hideMenu}>
        <MenuItem onPress={hideMenu}>Menu item 1</MenuItem>
        <MenuItem onPress={hideMenu}>Menu item 2</MenuItem>
        <MenuItem disabled>Disabled item</MenuItem>
        <MenuDivider />
        <MenuItem onPress={hideMenu}>Menu item 4</MenuItem>
      </Menu>
    </View>
  );
}
  • Related