I am using React-native and the <Picker />
component.
The problem I am having, is that the value='1'
is a string, but I would like it to return a number. I have seen ways to parse it, but I am unsure how to do this in react-native.
class TestPage extends Component {
constructor(props){
super(props);
this.state = {
myNumber:0
}
}
componentDidMount(){
}
render(){
console.log('My value : ',this.state.myNumber);
return (
<View>
<Picker
selectedValue={this.state.myNumber}
style={{ height: 50, width: 150 }}
onValueChange={(itemValue) => this.setState({myNumber: itemValue})}>
<Picker.Item label="One" value="1" />//The value here
<Picker.Item label="Two" value="2" />
</Picker>
</View>
);
}
}
Thank you.
对我来说,更好的方法- 使用一元“ +” :
this.setState({myNumber: +itemValue})
尝试使用以下内容:
onValueChange={(itemValue) => this.setState((itemValue)=>{myNumber: Number(itemValue)})}>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.