gpt4 book ai didi

javascript - React-Native 自定义组件不会触发 onValueChange、onPress 或 onChange

转载 作者:行者123 更新时间:2023-11-28 04:10:50 26 4
gpt4 key购买 nike

我是 React-Native 新手,在使用自定义组件时遇到问题。该组件是一个自定义时间选择器,返回所选的小时。我的问题是,我无法弄清楚如何使用所选值正确更新状态,因为选择值不会触发 onValueChange、onPress 或 onChange 事件。

这里是 TimePicker 实际显示的位置 - 我目前将其设置为仅在 Press 上抛出警报(尽管我也尝试过 onValueChange、onPress 和 onChange)。但是,当您选择时间时,没有任何反应:

            <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;

最佳答案

尝试直接在 Prop 内部使用该函数。例如,它需要读取:

onPress={(value)=>{
this.setState({
...this.state,
selectedKey: item.key
})
}}

Prop 运行函数的方式可能存在问题。

关于javascript - React-Native 自定义组件不会触发 onValueChange、onPress 或 onChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46289004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com