Javascript React本机自定义组件不会触发onValueChange、onPress或onChange
我是React本地新手,在使用自定义组件时遇到问题。该组件是一个自定义时间选择器,返回所选的小时数。我的问题是,我不知道如何使用所选值正确更新状态,因为选择值不会触发onValueChange、onPress或onChange事件 这里是时间选择器实际显示的位置-我目前将其设置为只在按下时抛出警报,尽管我也尝试了onValueChange、onPress和onChange。但是,当您选择一个时间时,不会发生任何事情:Javascript React本机自定义组件不会触发onValueChange、onPress或onChange,javascript,react-native,Javascript,React Native,我是React本地新手,在使用自定义组件时遇到问题。该组件是一个自定义时间选择器,返回所选的小时数。我的问题是,我不知道如何使用所选值正确更新状态,因为选择值不会触发onValueChange、onPress或onChange事件 这里是时间选择器实际显示的位置-我目前将其设置为只在按下时抛出警报,尽管我也尝试了onValueChange、onPress和onChange。但是,当您选择一个时间时,不会发生任何事情: <TimePicker onPress={/*(
<TimePicker onPress={/*(value) => this.setState({shiftStart: this.state.selectedKey})*/() => this.testme() } />
<TimePicker onValueChange={(value) => this.setState({shiftEnd: value})} style={{marginLeft : 100}}/>
以下是自定义组件:
import React, { Component } from 'react';
import { View , Image, Text, TouchableHighlight, Animated, ScrollView,
Easing, Alert} from 'react-native';
const up_image = require('./up.png');
const down_image = require('./down.png');
const all_times = [
{ key : '0', title : '12:00 AM'},
{ key : '1', title : '1:00 AM'},
{ key : '2', title : '2:00 AM'},
{ key : '3', title : '3:00 AM'},
{ key : '4', title : '4:00 AM'},
{ key : '5', title : '5:00 AM'},
{ key : '6', title : '6:00 AM'},
{ key : '7', title : '7:00 AM'},
{ key : '8', title : '8:00 AM'},
{ key : '9', title : '9:00 AM'},
{ key : '10', title : '10:00 AM'},
{ key : '11', title : '11:00 AM'},
{ key : '12', title : '12:00 PM'},
{ key : '13', title : '1:00 PM'},
{ key : '14', title : '2:00 PM'},
{ key : '15', title : '3:00 PM'},
{ key : '16', title : '4:00 PM'},
{ key : '17', title : '5:00 PM'},
{ key : '18', title : '6:00 PM'},
{ key : '19', title : '7:00 PM'},
{ key : '20', title : '8:00 PM'},
{ key : '21', title : '9:00 PM'},
{ key : '22', title : '10:00 PM'},
{ key : '23', title : '11:00 PM'},
]
class TimePicker extends Component {
constructor(props){
super(props);
this.state = {
selectedKey : '0',
scroll_pos : 0,
}
this.animValue = new Animated.Value(0);
this.timeSelected = this.timeSelected.bind(this);
this.do_up = this.do_up.bind(this);
this.do_down = this.do_down.bind(this);
}
animate () {
this.animValue.setValue(0);
Animated.timing(
this.animValue,
{
toValue: 1,
duration: 2000,
easing: Easing.linear
}
).start((o) => {
if(!o.finished)
this.animate();
})
}
do_up(){
if(this.state.scroll_pos >= 20)
this._scrollview.scrollTo({x : 0, y : this.state.scroll_pos - 20, animated : true});
}
do_down(){
if(this.state.scroll_pos <= 390)
this._scrollview.scrollTo({x : 0, y : this.state.scroll_pos + 20, animated : true});
}
get_newTimeStatus(newPos){
var tm_st = [];
for(var i = 0; i < all_times.length ; i ++){
if(all_times[i].key == newPos){
for(var j = i; j < i+6;j ++){
tm_st.push(all_times[j]);
}
}
}
console.log(newPos);
return tm_st;
}
timeSelected(timeKey){
this.setState({
...this.state,
selectedKey : timeKey,
})
}
render() {
const marginTop = this.animValue.interpolate({
inputRange: [0, 1],
outputRange: [0, 23]
})
return (
<View style={{ ...this.props.style, width : 70, height : 180, flexDirection : 'column'}}>
<TouchableHighlight
style={{width : 70, height : 15, alignItems : 'center'}}
onPress = {this.do_up.bind(this)}
underlayColor = 'transparent'
>
<Image source={up_image} style={{width : 15, height : 15, resizeMode : 'stretch'}}/>
</TouchableHighlight>
<ScrollView style={{width : 70,
height : 140,
marginTop : 5,
backgroundColor : '#345777',
flexDirection : 'column'}}
ref = {(ref) => {
this._scrollview = ref;
}}
scrollEventThrottle = {20}
onScroll = {event => {
this.setState({
scroll_pos : event.nativeEvent.contentOffset.y
})
}}
showsHorizontalScrollIndicator={false} >
{
all_times.map(item =>
(<View style={{width : 70,
height : 23,
alignItems : 'center',
justifyContent : 'center',
borderBottomWidth : 1,
borderColor : 'white',
backgroundColor : item.key === this.state.selectedKey ? '#47C6CD' : '#345777'}}
key={item.key}>
<TouchableHighlight
underlayColor = 'transparent'
onPress={() => {this.timeSelected(item.key)}}
>
<Text style={{color : 'white', }}>{item.title}</Text>
</TouchableHighlight>
</View>)
)
}
</ScrollView>
<TouchableHighlight
style={{width : 70, height : 15,marginTop : 5, alignItems : 'center', justifyContent : 'center'}}
onPress = {this.do_down.bind(this)}
underlayColor = 'transparent'
>
<Image source={down_image} style={{width : 15, height : 15, resizeMode : 'stretch'}}/>
</TouchableHighlight>
</View>
);
}
}
export default TimePicker;
尝试直接在道具内部使用该功能。例如,它需要阅读:
onPress={(value)=>{
this.setState({
...this.state,
selectedKey: item.key
})
}}
道具运行函数的方式可能存在问题。请尝试直接在道具内部使用该函数。例如,它需要阅读:
onPress={(value)=>{
this.setState({
...this.state,
selectedKey: item.key
})
}}
道具如何运行函数可能存在问题