I can't get the onchange function typing correct. I created a handler function but typescript keeps complaining about type mismatch.
My function:
private handleChange(options: Array<{label: string, value: number}>) {
}
Typescript error:
src/questionnaire-elements/AssessmentFactorSearch.tsx (43,9): Type '{ ref: "select"; name: string; backspaceToRemoveMessage: ""; value: { label: string; value: IAsse...' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Async<OptionValues>> & Readonly<{ children?: React...'.
Type '{ ref: "select"; name: string; backspaceToRemoveMessage: ""; value: { label: string; value: IAsse...' is not assignable to type 'Readonly<ReactAsyncSelectProps<OptionValues>>'.
Types of property 'onChange' are incompatible.
Type '(options: { label: string; value: number; }[]) => void' is not assignable to type 'OnChangeHandler<OptionValues, Option<OptionValues> | Option<OptionValues>[]> | undefined'.
Type '(options: { label: string; value: number; }[]) => void' is not assignable to type 'OnChangeHandler<OptionValues, Option<OptionValues> | Option<OptionValues>[]>'. (2322)
How do I type the onchange method?
To handle the onChange event on a select element in React: Set the onChange prop on the select element. Keep the value of the selected option in a state variable. Every time the user changes the selected option, update the state variable.
To type the onChange event of an element in React, set its type to React. ChangeEvent<HTMLInputElement> . The ChangeEvent type has a target property which refers to the element. The element's value can be accessed on event.
You can clear the value of react select using the ref. Just store the value in the state, and change the state programmatically using componentDidUpdate etc... Note: 'value' should be an object. A simple option would be to pass null to the value prop.
Based on https://github.com/JedWatson/react-select/issues/2902, just write your handler like this:
// define your option type
type MyOption = {label: string, value: number}
// ...
// define your onChange handler:
private handleChange = (selected?: MyOption | MyOption[] | null) => {
/** Code **/
}
Take note of the part MyOption | MyOption[] | null
, which matches the same structure as the definition of onChange
in the react-select library. Just make your own MyOption
type.
Another example in that issue thread also shows that you can pass the function inline, and in that case the type of the handler parameter is inferred automatically:
<Select
onChange={selectedOption /* type is automatically inferred here */ => {
if (Array.isArray(selectedOption)) {
throw new Error("Unexpected type passed to ReactSelect onChange handler");
}
doSomethingWithSelectedValue(selectedOption.value);
}}
...
/>
as for react-select ˆ3.1.0 the code below should work:
import {ValueType, ActionMeta} from 'react-select';
type MyOptionType = { label: string, value: number }
type OnChange = (value: ValueType<MyOptionType>, actionMeta: ActionMeta<MyOptionType>) => void;
2021 answer:
Handler:
const addTagHandler = (tags: OnChangeValue<TagOptionType, true>) => {
if (tags) {
setTags((tags as TagOptionType[]).map((tag: TagOptionType) => tag.value));
}
};
Where:
As follows:
type TagOptionType = { label: string, value: string }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With