Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define an onChange handler for React-Select in typescript

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?

like image 911
Michael Avatar asked Dec 11 '17 20:12

Michael


People also ask

How do I use onChange in React select?

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.

How do I use onChange event in TypeScript?

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.

How do you clear the selected value in React select?

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.


3 Answers

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);
  }}
  ...
/>
like image 199
daliusd Avatar answered Oct 22 '22 10:10

daliusd


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;
like image 41
Sergei Kurochkin Avatar answered Oct 22 '22 10:10

Sergei Kurochkin


2021 answer:

  1. Don't use the @types/react-select lib, it's been deprecated
  2. onChange handler

Handler:

const addTagHandler = (tags: OnChangeValue<TagOptionType, true>) => {
    if (tags) {
      setTags((tags as TagOptionType[]).map((tag: TagOptionType) => tag.value));
    }
  };

Where:

  1. boolean true refers to isMulti (are there multiple elements, or single)
  2. Where TagOptionType looks

As follows:

type TagOptionType = { label: string, value: string }
like image 2
html_programmer Avatar answered Oct 22 '22 12:10

html_programmer