score:12
The onChange
callback from react-datepicker
looks like this:
onChange(date: Date | null, event: React.SyntheticEvent<any> | undefined): void;
So date
potentially can be null
. You have two options here:
1.) accept nullable date state in useState
Hook:
const [startDate, setStartDate] = useState<Date | null>(new Date());
2.) only invoke setStartDate
, when date
is not null:
<DatePicker selected={startDate} onChange={date => date && setStartDate(date)} />
score:1
This works for me:
() => {
const [startDate, setStartDate] = useState<Date|null>(new Date());
return (
<DatePicker selected={startDate} onChange={(date: Date | null) => setStartDate(date)} />
);
};
score:2
I know I am late to join the party, I just got the DatePicker working. Following is the working code:
const [startDate, setStartDate] = useState<Date>(new Date());
<DatePicker
selected={startDate}
onChange={(date: Date) => setStartDate(date!)}
/>
score:8
Update for same issue in 2021
<DatePicker selected={startDate} onChange={(date: Date | null) => setStartDate(date)} />
Does the job
Source: stackoverflow.com
Related Query
- TS2345: Argument of type 'Date | null' is not assignable to parameter of type 'string | Date | undefined'
- React Typescript - Argument of type is not assignable to parameter of type
- Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element'. Type 'null' is not assignable to type 'Element'.ts(2345)
- Argument of type 'unknown' is not assignable to parameter of type '{}'
- Argument of type 'Element' is not assignable to parameter of type 'ReactElement<any>
- react usestate hooks error: argument of type 'yyy' is not assignable to parameter of type 'setstateaction<xx>'
- getting error : Argument of type '() => () => boolean' is not assignable to parameter of type 'EffectCallback'
- Typescript React/Redux : Argument of type 'typeof MyClass' is not assignable to parameter of type 'ComponentType<...'
- React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)'
- Argument of type '"MY_EVENTS_LOAD"' is not assignable to parameter of type 'TakeableChannel<unknown>' in yeild takeLatest
- React TypeScript: Argument is not assignable to parameter of type 'never'
- Argument of type 'string' is not assignable to parameter of type '`${string}` | `${string}.${string}` | `${string}.${number}`'
- Argument of type '(dispatch: Dispatch) => void' is not assignable to parameter of type 'AnyAction'
- Argument of type partial is not assignable to parameter of type
- Argument of type 'Date | null' is not assignable to parameter of type 'SetStateAction<Date>'
- React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type
- Argument of type 'number' is not assignable to parameter of type 'never' in array.includes()
- Argument of type 'string | null' is not assignable to parameter of type 'ValueFn<SVGPathElement, Datum[], string | number | boolean | null>'
- Argument of type '() => () => Promise<void>' is not assignable to parameter of type 'EffectCallback'
- Getting an error Argument of type 'unknown' is not assignable to parameter of type 'Error | null'
- Redux Toolkit - Argument of type 'AsyncThunkAction<>' is not assignable to parameter of type 'AnyAction'
- React js Typescript Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'
- Argument of type 'Element[]' is not assignable to parameter of type 'Element'
- Redux - createStore. Argument of type is not assignable to parameter of type 'DeepPartial<any>'
- Argument of type 'AsyncThunkAction<any, void, {}>' is not assignable to parameter of type 'AnyAction'
- TS2345: Argument of type 'ReactNode' is not assignable to parameter of type 'ReactElement'
- Argument of type 'string | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'
- Argument of type 'typeof test' is not assignable to parameter of type 'Component<{ children?: ReactNode; } & DispatchProp<any>>'
- React onclick Argument of type 'EventTarget' is not assignable to parameter of type 'Node'
- TypeScript w/ React - Argument of type 'string' is not assignable to parameter of type 'keyof T'
More Query from same tag
- change event not firing from external script on react page
- use .map during component composition in reactjs
- React TypeScript how to pass property to child component
- How to format specific words in a sentence in react-intl
- component not re-rendering after redux state change
- Nextjs - Access global context values
- I am trying to update an array declared with useState, but I am getting the error 'Too many re-renders'
- How to filter react-chartjs-2
- How to implement Axios.delete in react and delete from Mongodb by id
- What's the best practice with React hooks if I need to do some stateful orchestration?
- Shallow from Enzyme not rendering the jxs component and just returns an empty object
- How to set a class name for a div dynamically in react js
- React-Redux: deleting an item does not re-render the array
- dynamically add item VS scroll behavior
- Webpack module build failed unexpected token (rails react build)
- Using redux with redux-persist with server-side rendering
- firebase not return the key?
- JSX, hide an element using inline ternary operator?
- How to initialize state with api's data
- redux: nested reducer access another store section
- Convert URL Input to Image
- hide Navigation-Bar in reactJS
- TypeError: Cannot read properties of undefined (reading 'preventDefault')
- Fabric UI Using PivotItem in Map Loop
- React JS - Event Handler in a dynamic list
- Reactjs website with admin parts
- React Hooks - Prevent re-render when using prevState
- Adding promise polyfill to ES6
- TypeError: movies is undefined using React Hooks
- Function not passing in argument - React Redux