gpt4 book ai didi

javascript - 让我的日期选择器传递他的值(value)时遇到麻烦

转载 作者:行者123 更新时间:2023-12-02 22:12:47 25 4
gpt4 key购买 nike

我正在学习 React Native,我试图将日期从我的日期选择器组件传递到表单,但我无法这样做。我尝试在表单上创建日期选择器,但由于我的表单卡在钩子(Hook)上,我无法这样做。

这是日期选择器的代码

import DatePicker from 'react-native-datepicker';
//import DatePicker from the package we installed

export default class MyDatePicker extends Component {

constructor(props) {
super(props)
//set value in state for initial date
this.state = {
date: "15-05-2018"
}
}


render() {
return ( <View style = {
styles.container
}>

<DatePicker date = {
this.state.date
} //initial date from state
mode = "date" //The enum of date, datetime and time
placeholder = "select date"
format = "DD-MM-YYYY"
minDate = "01-01-1900"
maxDate = "01-01-2019"
confirmBtnText = "Confirm"
cancelBtnText = "Cancel"
androidMode = "spinner"
onDateChange = {
(date) => {
this.setState({
date: date
}, console.log({
date: date
}))
}
}
/>

</View>
)
}
}

日期选择器工作,控制台日志在字符串上显示日期,问题似乎出在我的表单和获取值的方式上

import MyDatePicker from './MyDatePicker';

const PersonalForm = ({onSubmit, errorMessage}) => {
const [vName, setvName] = useState('');
const [vSecondName, setvSecondName] = useState('');
const [vBirthDate, setvBirthDate] = useState('');

return (
<ScrollView>
<View style={styles.buttonContainer}>
<View style={styles.inputContainer}>
<TextInput style={styles.inputs}
placeholder="Nombre"
underlineColorAndroid='transparent'
onChangeText={newvName => setvName(newvName)}
value={vName}
autoCorrect={false}
autoCapitalize='characters'
/>
</View>
<View style={styles.inputContainer}>
<TextInput style={styles.inputs}
placeholder="Segundo nombre"
underlineColorAndroid='transparent'
onChangeText={newvSecondName => setvSecondName(newvSecondName)}
value={vSecondName}
autoCorrect={false}
autoCapitalize='characters'
/>
</View>

<View>
<MyDatePicker vBirthDate={date} />
</View>
</View>
<View style={styles.buttonContainer2}>
<TouchableOpacity
style={ styles.logout}
onPress={() => onSubmit(vName, vSecondName, vBirthDate), console.log(vName, vSecondName, vBirthDate)}
>
<Text style={styles.loginText}>GUARDAR</Text>
</TouchableOpacity>
</View>
</ScrollView>
);
};

最佳答案

您的状态在 MyDatePicker 内发生变化,但在 PersonalForm 内不会发生变化,您需要在 PersonalForm 内设置它,因为您将 vBirthDate 发送到 PersonalForm 内的表单。
我建议更改 PersonalForm:。
首先在顶部导入react-native-datepicker,然后:
在 PersonalForm 中,您将 MyDatePicker 删除并添加以下内容:

<DatePicker 
date={vBirthDate} //initial date from state
mode="date" //The enum of date, datetime and time
placeholder="select date"
format="DD-MM-YYYY"
minDate="01-01-1900"
maxDate="01-01-2019"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
androidMode="spinner"
onDateChange={(date) => setvBirthDate(date)}
/>

关于javascript - 让我的日期选择器传递他的值(value)时遇到麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59505445/

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