react-native-datepicker
react-native-datepicker copied to clipboard
How to open date picker manually
Issue
Hi, I am unable to open date picker manually. Please provide solution for this problem. I am sharing code snippet below.
Code
openDatePicker(date, mode, format, index) {
this.setState({ datePickerDate: date, datePickerMode: mode, datePickerFormat: format });
DatePicker.onPressDate;
}
and I am calling this method (openDatePicker) on row click.
Environment
-
react-native -v
: 0.47.1 -
node -v
:6.2.1 -
npm -v
:4.2.0 -
target platform
: Android -
operating system
: windows 10 64 bit
the above solution doesn't work. I'm running into this problem too. I'd like to display the date picker from my custom button. How do you show/hide the date picker on a specific event?
use: <DatePicker showIcon={false} hideText={true} ref={(ref)=>this.datePickerRef=ref} ... />
and from your element: onPress={() => this.datePickerRef.onPressDate()
EyalSi's method worked for me.
Here is how you can open it manually with hooks:
import { useRef } from 'react';
// ...
const datePickerRef = useRef(null);
// ...
<SomeComponent onPress={() => datePickerRef.current.onPressDate()} />
// ...
<DatePicker showIcon={false} hideText={true} ref={datePickerRef} />
Here is how you can open it manually with hooks:
import { useRef } from 'react'; // ... const datePickerRef = useRef(null); // ... <SomeComponent onPress={() => datePickerRef.current.onPressDate()} /> // ... <DatePicker showIcon={false} hideText={true} ref={datePickerRef} />
Thanks a lot, clean solution.
@EyalSi I did exactly that and I get an error saying "this.ref.onPressDate is not a function...
Here is how you can open it manually with hooks:
import { useRef } from 'react'; // ... const datePickerRef = useRef(null); // ... <SomeComponent onPress={() => datePickerRef.current.onPressDate()} /> // ... <DatePicker showIcon={false} hideText={true} ref={datePickerRef} />
@EyalSi I'm using typescript. what should be the typing of ref? Any suggestion?
@aliwaqar981, you can import DatePicker and use it as the type as
const datePickerRe = useRef<DatePicker>(null)