I’m trying to use a datepiker in react native to consolelog the piked date when I click on the Button i want to console.log(slected) selected date
const [text, setTextname] = useState();
const [mydate, setDate] = useState(new Date());
const [displaymode, setMode] = useState('date');
const [isDisplayDate, setShow] = useState(false);
const changeSelectedDate = (event, selectedDate) => {
const currentDate = selectedDate || mydate;
let date = moment(selectedDate).format('DD/MM/YYYY');
console.log('dadad', date);
setTextname(date);
setShow(false);
};
const showMode = currentMode => {
setShow(true);
setMode(currentMode);
};
const displayDatepicker = () => {
showMode('date');
};
function getdata() {
console.log('sdsds', text);
}
<Button onPress={displayDatepicker} title="Show date picker!" />
{isDisplayDate && (
<DateTimePicker
testID="dateTimePicker"
value={mydate}
mode={displaymode}
is24Hour={true}
display="default"
onChange={text => changeSelectedDate()}
// onChange={changeSelectedDate}
/>
)}
<View style={styles.buttonw}>
<Button
color="#7743DB"
title="Lets Go"
type="submit"
onPress={() => getdata()}
/>
</View>
i have displayed the datepiker but i am not able to pick a date and pass it to getdata
2
Answers
You have to set date on setDate state in your change date function.
Remove
Replace in function changeSelectedDate
with
and
by
and lastly
changes to function