xccelerated99
xccelerated99

Reputation: 23

Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string' - React Native

I just installed this DropDownPicker UI component in React Native (source: https://hossein-zare.github.io/react-native-dropdown-picker-website/docs/usage).

Here's the code:

  const [frequency, setFrequency] = useState("");
  const [open, setOpen] = useState(false);
  const [value, setValue] = useState(null);
  const [items, setItems] = useState([
    {label: 'Daily', value: 'Daily'},
    {label: 'Weekly', value: 'Weekly'},
    {label: 'BiWeekly', value: 'BiWeekly'},
    {label: 'Monthly', value: 'Monthly'},
  ]);

....blah blah blah....

    <DropDownPicker
            open={open}
            value={value}
            items={items}
            setOpen={setOpen}
            setValue={setValue}
            setItems={setItems}
            onChangeValue={(value) => {
              console.log(value);
              setFrequency(value);
              console.log(frequency);
            }}
            style={{marginLeft:10,width:'90%', marginBottom:10}}
      />

Intellisense complains on the line where I am trying to call setFrequency(value) with the error mentioned in the Title of this question. I need to set the value of frequency to the value selected in the DropDownPicker. I'm not sure how to fix this. Any ideas?

Upvotes: 0

Views: 34

Answers (1)

Vicky Ahuja
Vicky Ahuja

Reputation: 1214

As per the interface declaration of file https://github.com/hossein-zare/react-native-dropdown-picker/blob/5.x/index.d.ts of this DropDown Picker library

onChangeValue function can receive any generic type as well as null value in its argument.

Here you can simply use as below method to fix this Intellisense warning.

setFrequency(value as string);

Upvotes: 0

Related Questions