带有 Formik 的 React-native-datepicker
React-native-datepicker with Formik
我正在尝试使用 Formik 在提交表单时从 React-native-datepicker 获取值,但是,我只得到空的并且似乎无法弄清楚如何传递这些值。我是 react-native 和 Formik 的新手,非常感谢任何帮助。我只是想让结果记录在表单提交上。我也尝试过 react hook 形式,但没有成功。
我的App.js代码:
import React, {useState} from 'react';
import {View, Text, StyleSheet, TextInput, Button, Alert} from 'react-native';
import { Formik } from 'formik';
import DatePicker from 'react-native-datepicker';
import { DateInput } from 'react-native-date-input';
const App = props =>{
const [date, setDate] = useState(new Date())
const [date2, setDate2] = useState(new Date())
return(
<Formik
initialValues={{ date: '', date2: '' }}
onSubmit={values => console.log(values)}
>
{({ onDateChange, handleBlur, handleSubmit, values }) => (
<View>
<Text style={styles.selectDate}>Lab Collection Date</Text>
<DatePicker
style={{width: 200, alignSelf: 'center'}}
date={date}
mode="date"
placeholder="select date"
format="DD MMM YYYY"
minDate="01 Jan 2021"
maxDate="30 Dec 2025"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36,
borderRadius:4
}
// ... You can check the source to find the other keys.
}}
onDateChange={(newDate) => {setDate(newDate)}}
value={values.date}
onBlur={handleBlur('date')}
/>
<Text style={styles.selectDate}>Symptom Onset Date</Text>
<DatePicker
style={{width: 200, alignSelf: 'center'}}
date={date2}
mode="date"
placeholder="select date"
format="DD MMM YYYY"
minDate="01 Jan 2021"
maxDate="30 Dec 2025"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36,
borderRadius:4
}
// ... You can check the source to find the other keys.
}}
onDateChange={(newDate2) => setDate2(newDate2)}
value={values.date2}
/>
<Button onPress={handleSubmit} title="Submit" />
</View>
)}
</Formik>
)
};
const styles = StyleSheet.create({
selectDate:{
fontFamily:'open-sans',
fontSize:20,
marginTop: 50,
marginBottom:10,
alignSelf:'center',
color:'red'
},
datebox:{
alignSelf:'center',
height:50,
width:500,
}
});
export default App;
使用来自 DatePicker 的 formik onDateChange 的 setFieldValue('date1', value)。
const App = props => {
const [date, setDate] = useState(new Date())
const [date2, setDate2] = useState(new Date())
return (
<Formik
initialValues={{ date: '', date2: '' }}
onSubmit={values => console.log(values)}
>
{({ onDateChange, handleBlur, handleSubmit, setFieldValue, values }) => (
<View>
<Text style={styles.selectDate}>Lab Collection Date</Text>
<DatePicker
style={{ width: 200, alignSelf: 'center' }}
date={date}
mode="date"
placeholder="select date"
format="DD MMM YYYY"
minDate="01 Jan 2021"
maxDate="30 Dec 2025"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36,
borderRadius: 4
}
// ... You can check the source to find the other keys.
}}
onDateChange={(newDate) => { setFieldValue('date', newDate) }}
value={values.date}
onBlur={handleBlur('date')}
/>
<Text style={styles.selectDate}>Symptom Onset Date</Text>
<DatePicker
style={{ width: 200, alignSelf: 'center' }}
date={date2}
mode="date"
placeholder="select date"
format="DD MMM YYYY"
minDate="01 Jan 2021"
maxDate="30 Dec 2025"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36,
borderRadius: 4
}
// ... You can check the source to find the other keys.
}}
onDateChange={(newDate2) => setFieldValue('date2', newDate2)}
value={values.date2}
/>
<Button onPress={handleSubmit} title="Submit" />
</View>
)}
</Formik>
)
};
const styles = StyleSheet.create({
selectDate: {
fontFamily: 'open-sans',
fontSize: 20,
marginTop: 50,
marginBottom: 10,
alignSelf: 'center',
color: 'red'
},
datebox: {
alignSelf: 'center',
height: 50,
width: 500,
}
});
export default App;
我正在尝试使用 Formik 在提交表单时从 React-native-datepicker 获取值,但是,我只得到空的并且似乎无法弄清楚如何传递这些值。我是 react-native 和 Formik 的新手,非常感谢任何帮助。我只是想让结果记录在表单提交上。我也尝试过 react hook 形式,但没有成功。
我的App.js代码:
import React, {useState} from 'react';
import {View, Text, StyleSheet, TextInput, Button, Alert} from 'react-native';
import { Formik } from 'formik';
import DatePicker from 'react-native-datepicker';
import { DateInput } from 'react-native-date-input';
const App = props =>{
const [date, setDate] = useState(new Date())
const [date2, setDate2] = useState(new Date())
return(
<Formik
initialValues={{ date: '', date2: '' }}
onSubmit={values => console.log(values)}
>
{({ onDateChange, handleBlur, handleSubmit, values }) => (
<View>
<Text style={styles.selectDate}>Lab Collection Date</Text>
<DatePicker
style={{width: 200, alignSelf: 'center'}}
date={date}
mode="date"
placeholder="select date"
format="DD MMM YYYY"
minDate="01 Jan 2021"
maxDate="30 Dec 2025"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36,
borderRadius:4
}
// ... You can check the source to find the other keys.
}}
onDateChange={(newDate) => {setDate(newDate)}}
value={values.date}
onBlur={handleBlur('date')}
/>
<Text style={styles.selectDate}>Symptom Onset Date</Text>
<DatePicker
style={{width: 200, alignSelf: 'center'}}
date={date2}
mode="date"
placeholder="select date"
format="DD MMM YYYY"
minDate="01 Jan 2021"
maxDate="30 Dec 2025"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36,
borderRadius:4
}
// ... You can check the source to find the other keys.
}}
onDateChange={(newDate2) => setDate2(newDate2)}
value={values.date2}
/>
<Button onPress={handleSubmit} title="Submit" />
</View>
)}
</Formik>
)
};
const styles = StyleSheet.create({
selectDate:{
fontFamily:'open-sans',
fontSize:20,
marginTop: 50,
marginBottom:10,
alignSelf:'center',
color:'red'
},
datebox:{
alignSelf:'center',
height:50,
width:500,
}
});
export default App;
使用来自 DatePicker 的 formik onDateChange 的 setFieldValue('date1', value)。
const App = props => {
const [date, setDate] = useState(new Date())
const [date2, setDate2] = useState(new Date())
return (
<Formik
initialValues={{ date: '', date2: '' }}
onSubmit={values => console.log(values)}
>
{({ onDateChange, handleBlur, handleSubmit, setFieldValue, values }) => (
<View>
<Text style={styles.selectDate}>Lab Collection Date</Text>
<DatePicker
style={{ width: 200, alignSelf: 'center' }}
date={date}
mode="date"
placeholder="select date"
format="DD MMM YYYY"
minDate="01 Jan 2021"
maxDate="30 Dec 2025"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36,
borderRadius: 4
}
// ... You can check the source to find the other keys.
}}
onDateChange={(newDate) => { setFieldValue('date', newDate) }}
value={values.date}
onBlur={handleBlur('date')}
/>
<Text style={styles.selectDate}>Symptom Onset Date</Text>
<DatePicker
style={{ width: 200, alignSelf: 'center' }}
date={date2}
mode="date"
placeholder="select date"
format="DD MMM YYYY"
minDate="01 Jan 2021"
maxDate="30 Dec 2025"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateIcon: {
position: 'absolute',
left: 0,
top: 4,
marginLeft: 0
},
dateInput: {
marginLeft: 36,
borderRadius: 4
}
// ... You can check the source to find the other keys.
}}
onDateChange={(newDate2) => setFieldValue('date2', newDate2)}
value={values.date2}
/>
<Button onPress={handleSubmit} title="Submit" />
</View>
)}
</Formik>
)
};
const styles = StyleSheet.create({
selectDate: {
fontFamily: 'open-sans',
fontSize: 20,
marginTop: 50,
marginBottom: 10,
alignSelf: 'center',
color: 'red'
},
datebox: {
alignSelf: 'center',
height: 50,
width: 500,
}
});
export default App;