How to close react native popup menu? - react-native

I'm using your react-native-popup-menu for logout button.
when button clicked the authentication deleted and screen will go to login .
but the menu still left.
How to close this menu when screen switched?
<Menu>
<MenuTrigger>
<Icon
name='more-vert'
color='#fff'
/>
</MenuTrigger>
<MenuOptions>
<MenuOption value={1}>
<Text onPress={() => {
this.props.onLogout()
}}>logout</Text>
</MenuOption>
</MenuOptions>
</Menu>
Originally asked by bexoss on react-native-popup-menu GitHub.

All other answers here will work. Here is another (simpler) solution to the problem depending on what are your requirements.
You are handling logout event in Text component and therefore handlers to close menu are not triggered. Try to pass it to the onSelect property of MenuOption:
<MenuOption onSelect={() => this.props.onLogout()}>
<Text>logout</Text>
</MenuOption>
Note: If you returned false from your handler, menu would not close.

https://github.com/instea/react-native-popup-menu/blob/master/doc/api.md
The API document above states that there is a close() method.
So what you need to do is just declare the ref attribute to access your Menu component directly. E.g "menuRef":
<Menu uref='menuRef'>
<MenuTrigger>
<Icon
name='more-vert'
color='#fff'
/>
</MenuTrigger>
<MenuOptions>
<MenuOption value={1}>
<Text onPress={() => {
this.props.onLogout()
}}>logout</Text>
</MenuOption>
</MenuOptions>
</Menu>
and then you can simply call from any where in your current component: this.refs.menuRef.close();
This approach will also animate the closing.

As mentioned in their documentation, you should use the visible prop in your Menu component.
You will need to adapt your code like this:
<Menu opened={this.state.opened}>
<MenuTrigger onPress={() => this.setState({ opened: true })}>
<Icon
name='more-vert'
color='#fff'
/>
</MenuTrigger>
<MenuOptions>
<MenuOption value={1}>
<Text onPress={() => {
this.props.onLogout()
this.setState({ opened: false })
}}>logout</Text>
</MenuOption>
</MenuOptions>
</Menu>
And I think you will need to define the default state of your component like this:
constructor(props) {
super(props)
this.state = { opened: false }
}
You can also find a full example here

Related

React Native DatePicker with TextInput

I am currently making a mobile app and part of it is collecting user data. At the minute I am stuck on getting the DatePicker to show on screen once the text field has been clicked on. I have tried to use the onFocus prop but it does not seem to be working and its not throwing any errors in the console. Here is the code that i have tried using:
const showDatePicker = () => {
return(
<DateTimePicker
value={date}
mode='date'
is24Hour={true}
display="spinner"
onChange={onChange}
/>
)
}
<View style={styles.inputContainer}>
<FontAwesome name="user-circle-o" color={'#808080'} size={20}/>
<TextInput onFocus={() => showDatePicker()} style={styles.input} placeholder="Date Of Birth"
/>
</View>
To give some additional context to the problem I am using a modal which acts as a form for collecting the data. Any help or tips appreciated.
You can wrap the TextInput with a <Pressable />.
<Pressable onPress={() => showDatePicker()}>
<TextInput />
</Pressable>

How to navigate from one menu option to other screen

I am using 'react-native-popup-menu'
<MenuOptions customStyles={{ optionText: styles.text }}>
<MenuOption value="History" text='History'
onPress={()=>{this.props.navigation.navigate('History')}} />
<MenuOption value="Logout" text='Logout' onPress={()=>{this.props.navigation.navigate('Login')}}/>
</MenuOptions>
I want to navigate when I click on any of the menu options to another screen,
how can I achieve it?
Looking at the docs from the Library, MenuOption takes an onSelect prop rather than onPress
From their example:
<MenuOptions>
<MenuOption **onSelect**={() => alert(`Save`)} text='Save' />
<MenuOption **onSelect**={() => alert(`Delete`)} >
<Text style={{color: 'red'}}>Delete</Text>
</MenuOption>
<MenuOption onSelect={() => alert(`Not called`)} disabled={true} text='Disabled' />
</MenuOptions>
Changing your onPress to onSelect should start working as per your requirements.

Popup menu does not overlay other components in Android

I am using react-native-popup-menu#0.10.0. It works fine on IOS but cannot overlay on another component on android. It is invisible on android. Please provide any solution .
renderHeader() {<Header><MenuContext style={styles.container}>
<View>
<Menu>
<MenuTrigger customStyles={triggerStyles} onPress={() => this.setState({ opened: true })}>
<FaIcon name='ellipsis-h' size={21} color='#b1b9bc'/>
</MenuTrigger>
<MenuOptions>
<MenuOption onSelect={() => this.edit()} disabled={this.state.disabled}>
<Text style={this.state.disabled ? {color: 'lightgrey'} : {color: 'black'}}>Edit</Text>
</MenuOption>
<MenuOption onSelect={() => this.delete()} disabled={this.state.disabled}>
<Text style={this.state.disabled ? {color: 'lightgrey'} : {color: 'red'}}>Delete</Text>
</MenuOption>
</MenuOptions>
</Menu>
</View>
</MenuContext></Header>}
This happened to me when the MenuContext wasn't high enough in the tree.
Eventually I moved it to the very first component's render function as the wrapper for everything, and that works great.

React-Native ActionButton auto trigger onPress without tapping button

All i need is to trigger onPress method of the ActionButton Without tapping on it.
Here is my code:
render () {
// act = this.refs.abc;
// alert(this.state.activeState)
// this.handleAutoPress();
return (
// <View style={styles.container}>
<View style={{flex:1, backgroundColor: '#f3f3f3'}} >
<Text>Component10 Component</Text>
{/*Rest of App come ABOVE the action button component!*/}
<ActionButton ref="abc" buttonColor="rgba(231,76,60,1)" >
{/*<ActionButton.Item buttonColor='#9b59b6' title="New Task" onPress={() =>
console.log("notes tapped!")}>
<Icon name="android-create" style={styles.actionButtonIcon} />
</ActionButton.Item>*/}
<ActionButton.Item buttonColor='#3498db' title="Notifications" onPress={() => {}}>
<Icon name="md-done-all" style={styles.actionButtonIcon} />
</ActionButton.Item>
<ActionButton.Item buttonColor='#1abc9c' title="All Tasks" onPress={() => {}}>
<Icon name="md-done-all" style={styles.actionButtonIcon} />
</ActionButton.Item>
</ActionButton>
</View>
// </View>
)
}
You probably want to follow the instructions in this answer: How can I trigger a tabItem onPress within a component using React Native?
Instead of trying to invoke the onPress itself, just set onPress prop to a function and invoke that function wherever needed. No need to mess with refs or anything like that.

React-Native:Objects are not valid as a React child (found: object with keys {pname})

Actually I developing Sample appilication,I want to displayed list of names using map function in react native menu option:
first i was declare one array:
after next i wrote map function in render() and that component used in return() at menu context Component.
var ListNames = [{"pname":"Smith"},{"pname":"Jhon"},{"pname":"Steve"},{"pname":"Ferr"},{"pname":"Kevin"}];
render(){
ListNames.map((ele, i)=>{
<Text>{ele.pname}</Text>
})
return(
<MenuContext style={{height:50,marginTop:20}} ref="MenuContext">
<Menu style={styles.dropdown} onSelect={(value) => this.setState({ dropdownSelection: value })}>
<MenuTrigger style={{flex:1,flexDirection:'row'}}>
<View style={{flex:.7}}>
<Text style={{color:'black',fontSize:15}}>{this.state.dropdownSelection}</Text>
</View>
<View style={{flex:.3,alignItems:'flex-end'}}>
<Image source = {require('./../images/dropdown.png')} style={{height:15,width:20,marginTop:3}}/>
</View>
</MenuTrigger>
<MenuOptions optionsContainerStyle={styles.dropdownOptions1}
renderOptionsContainer={(options) => <ScrollView><Text>SELECT....</Text>{options}</ScrollView>}>
<MenuOption value={ListNames}>
**{ListNames}**
</MenuOption>
{/* <MenuOption value="Kevin">
<Text>Kevin</Text>
</MenuOption>
<MenuOption value="Pieterson">
<Text>Pieterson</Text>
</MenuOption>
<MenuOption value="David">
<Text>David</Text>
</MenuOption>
<MenuOption value="Meissi">
<Text>Meissi</Text>
</MenuOption> */}
</MenuOptions>
</Menu>
</MenuContext>)
}
So I want using Mapping Function in react native menu option module is it Displaying are names using map function
any one please suggest this
Finally I Solved this , i am Very Foolish thinking, just in retturn methode
<MenuOption value={ListNames>
{ListNames}
</MenuOption>
replaced to given below
{
this.state.ListNames.map((ele, i)=>{
return <MenuOption value={ele.playerName}>
<Text>{ele.playerName}</Text>
</MenuOption>
})
}