Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I put a conditional here in the value?

I wanted to have the initial datepicker to get the date of today of which I can already do, at this point, selected2Date still does not exist. Then, I want to check in the value if selected2 was true and instead of displaying the date of today, it will display the date of selected2Date.

 const [selected2, setSelected2Date] = useState(new Date()); 

The code if selected2Date is still empty.

                          <MuiPickersUtilsProvider utils={DateFnsUtils}>
                                  <DatePicker
                                    format="MM/dd/yyyy"
                                    value={selected2}
                                    onChange={setSelected2Date}
                                    fullWidth
                                  />
                                </MuiPickersUtilsProvider>

The code if selected2Date is not empty. Can I put a conditional here in the value?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 <MuiPickersUtilsProvider utils={DateFnsUtils}>
                                      <DatePicker
                                        format="MM/dd/yyyy"
                                        value={user.choices?.selected2}
                                        onChange={setSelected2}
                                        fullWidth
                                      />
                                    </MuiPickersUtilsProvider>

>Solution :

If value exist in user.choices.selected2 then get from the user.choices.selected2 otherwise get from the selected2 !

try below code.

<MuiPickersUtilsProvider utils={DateFnsUtils}>
  <DatePicker
    format="MM/dd/yyyy"
    value={user.choices.selected2 ? user.choices.selected2 : selected2}
    onChange={setSelected2}
    fullWidth
  />
</MuiPickersUtilsProvider>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading