Skip to content

Instantly share code, notes, and snippets.

@sprobejames
Created July 8, 2024 06:53
Show Gist options
  • Select an option

  • Save sprobejames/1188f1b7705c3cf30d99ab530849fe55 to your computer and use it in GitHub Desktop.

Select an option

Save sprobejames/1188f1b7705c3cf30d99ab530849fe55 to your computer and use it in GitHub Desktop.
React Datepicker, MUI, RHF See https://stackoverflow.com/a/67995336
import { Controller, useForm } from 'react-hook-form'
import DatePicker from 'react-datepicker'
const App = () => {
const { control, register, handleSubmit } = useForm();
const onSubmit = (data) => {
alert(JSON.stringify(data));
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller
control={control}
name='date-input'
render={({ field }) => (
<DatePicker
placeholderText='Select date'
onChange={(date) => field.onChange(date)}
selected={field.value}
/>
)}
/>
<button type="submit">Submit</button>
</form>
)
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment