skip to Main Content

I’m having problems using DatePicker with react native
when I’m using it, there are an error like this:
render error a date or time must be specified as value prop
my repository: my github repository

const [date, setDate] = useState(new Date());

function changeDate(selectDate) {
    if (event?.type === 'dismissed') {
      setDate(date);
      return;
    }
    setDate(selectDate);
  }

<DatePicker
  format="DD/MM/YYY"
  date={date}
  onDateChange={changeDate}
 />

3

Answers


  1. You need to provide value prop instead of date

    <DatePicker format="DD/MM/YYY" value={date} onDateChange={changeDate} />
    
    Login or Signup to reply.
  2. maybe this will work

        const [date, setDate] = useState(new Date())
        
        const onChange = (event, selectedDate) => {
                setShowDate(false);
    
        
        if (event?.type === 'dismissed') {
            setDate(date);
            return;
        }
        setDate(selectedDate);
        };
    
    Login or Signup to reply.
  3. You used the wrong prop for the initial date it’s value not date

    and you also don’t need to set a date that is already set

    const [date, setDate] = useState(new Date());
    
    function changeDate(selectDate) {
        if (event?.type === 'dismissed') {
          return;
        }
        setDate(selectDate);
      }
    
    <DatePicker
      format="DD/MM/YYY"
      value={date}
      onDateChange={changeDate}
     />
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search