How to use Formik with React-Select isMulti

Viewed 38

I am trying to use this tutorial in my project, but with a slight difference: I want to use it with "isMulti". Can somebody tell me how I have to change the "onChange" to make this work?

1 Answers

You need to create a custom event and use it under onChange event.

<Formik
  initialValues={{
    tasks: state
  }}
  onSubmit={(values) => {
    console.log("values", values);
  }}
  enableReinitialize={true}
  render={({ values, errors, touched, handleReset, setFieldValue }) => {
    return (
      <Form className='w-100'>
        <Select
          isMulti={true}
          ref={selectInputRef ? selectInputRef : null}
          value={value}
          placeholder={placeholder}
          onChange={(selectedOption: any) => {
            let event = { target: { name: name, value: selectedOption } }
            setFieldValue(event);
          }}
          isSearchable={isSearchable}
          getOptionLabel={(option: any) => option.name}
          getOptionValue={(option: any) => option.id}
          options={options}
          name={name}
          isLoading={isLoading}
          loadingMessage={() => loadingMessage}
          noOptionsMessage={() => noOptionsMessage}
          isClearable={isClearable}
          id={id}
        />
      </Form>
   )}}
  />
Related