Add Children through array.forEach() to <MenuOptions> - react-native-popup-menu

I tried everything I can think of and I can't seem to have it working
I want to add multiple options from an array like this
<Menu renderer={renderers.SlideInMenu} ref={(ref) => statusRef = ref}>
<MenuTrigger/>
<TouchableWithoutFeedback onPress={this.onQuestionStatusButtonPressed}>
<View style={styles.view_header_submit_icon}>
<Icon style={styles.icon_header_submit} name={"dots-vertical"} />
</View>
</TouchableWithoutFeedback>
<MenuOptions>
{this.state.subjects.forEach((value) => {
<MenuOption >
<View style={styles.view_option}>
<Text style={styles.text_option}>value.name</Text>
</View>
</MenuOption>
)}
</MenuOption>
</MenuOptions>
</Menu>
This doesn't work for me
Am I doing something wrong? and the array has items in it because I debugged it with the console and it runs 2 times but never renders

Your code does not seem to be syntactically correct (e.g. extra </MenuOption>) and you have empty trigger but outside extra touchable..
Secondly - forEach won't work as it has no return value - use map instead.
I made simple working demo https://snack.expo.io/SJBJkr7yQ and it works without any problems
<Menu>
<MenuTrigger text="Select action" />
<MenuOptions>
<MenuOption onSelect={() => alert(`Save`)} text="Save" />
{this.state.subjects.map(s => <MenuOption key={s} text={s}/>)}
</MenuOptions>
</Menu>
Pro-tip: start with the simplest working example (eg. from official documentation) and add step-by-step things always checking that everything works.

Related

Text must be rendered within <Text> Component

While looking around at other questions, I still seemed to not be able to solve my issue. It still believes that there is text somewhere and I just need to know if I am completely overlooking something. I am new to React Native and React.js as a whole.
``
<View style={styles.body}>
{restaurantData.length ? (
<Text>{restaurantData.length}</Text>
) : null}
{restaurantData ? (
<View>
{restaurantData.map((restaurant) => (
<Cards restaurantData={restaurant} />
))}
</View>
) : null}
<Button onPress={getData} title="Get Data" />
<Divider style={{backgroundColor: 'blue'}} />;
<Button onPress={refreshData} title="Get Next 20 Restaurants" />
</View>
``
there's a ';' at the end of this link. Try removing it to see if that resolves your problem. If think I would check to see if there's extra spaces around your code. Sometimes React native doesn't like trailing spaces or spaces around your tags.
<Divider style={{backgroundColor: 'blue'}} />;

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.

How to close react native popup menu?

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

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>
})
}