Last active
December 12, 2017 08:43
-
-
Save nicolascouvrat/6a9046764231058604f1b0950f5374a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import { | |
| Picker, | |
| Alert, | |
| Button, | |
| View, | |
| } from 'react-native'; | |
| const Roulette = (props) => { | |
| const handlePress = () => { | |
| const randInt = Math.floor(Math.random() * 3); | |
| // this is where the magic happens :) | |
| props.onPress(randInt); | |
| }; | |
| return <Button title="Roulette" onPress={handlePress} />; | |
| }; | |
| class MyNativeSelect extends React.Component { | |
| constructor(props) { | |
| super(props); | |
| this.state = { | |
| selectedValue: 'Two', | |
| }; | |
| // don't forget the bindings | |
| this.handleValueChange = this.handleValueChange.bind(this); | |
| this.handleRoulette = this.handleRoulette.bind(this); | |
| } | |
| handleValueChange(selectedValue) { | |
| this.setState({ selectedValue }); | |
| Alert.alert(`selectedValue: ${selectedValue}`); | |
| } | |
| handleRoulette(index) { | |
| let selectedValue = ''; | |
| switch (index) { | |
| case 0: | |
| selectedValue = 'One'; | |
| break; | |
| case 1: | |
| selectedValue = 'Two'; | |
| break; | |
| default: | |
| selectedValue = 'Three'; | |
| } | |
| this.handleValueChange(selectedValue); | |
| } | |
| render() { | |
| return ( | |
| <View> | |
| <Picker | |
| selectedValue={this.state.selectedValue} | |
| onValueChange={this.handleValueChange} | |
| > | |
| <Picker.Item label="One" value="One" /> | |
| <Picker.Item label="Two" value="Two" /> | |
| <Picker.Item label="Three" value="Three" /> | |
| </Picker> | |
| <Roulette | |
| onPress={this.handleRoulette} | |
| /> | |
| </View> | |
| ); | |
| } | |
| } | |
| export default MyNativeSelect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment