Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird value changes when selecting multiple items. #737

Open
mersiades opened this issue Jan 18, 2024 · 4 comments
Open

Weird value changes when selecting multiple items. #737

mersiades opened this issue Jan 18, 2024 · 4 comments

Comments

@mersiades
Copy link

mersiades commented Jan 18, 2024

Hard to describe in words, but here's a video:

selecting-from-menu.mov

Looks like something is wrong with how the value state is being updated. I kept the video short, but the more I select the wilder it gets.

Minimum reproducible example is here: https://github.com/mersiades/rn-dropdown-picker-mre

export default function TabOneScreen() {
  const defaultItems = [
    { label: 'Australia', value: 'au' },
    { label: 'Indonesia', value: 'id' },
    { label: 'New Zealand', value: 'nz' },
  ];

  const [open, setOpen] = useState(false);
  const [value, setValue] = useState<string[]>([]);
  const [items, setItems] = useState(defaultItems);

  return (
    <View style={styles.container}>
      <DropDownPicker
        schema={{
          label: 'label',
          value: 'value',
        }}
        mode="BADGE"
        listMode={'MODAL'}
        value={value}
        items={items}
        setItems={setItems}
        open={open}
        setOpen={setOpen}
        setValue={(cb) => {
          const newValue = cb(value);
          setValue(newValue);
        }}
        multiple
        searchable
      />
    </View>
  );
}

I'm a bit surprised that no one else has raised this issue, which makes me suspect it's me that's doing something wrong, but if so, I've got no idea what it is.

@HugPet
Copy link

HugPet commented Feb 22, 2024

I've run into this issue as well. When logging, I've found that it is because the state transition is not working correctly. For example:

value -> newValue
[1, 2] -> [1] // remove 2
[1, 2] -> [2] // remove 1, but the state was not updated correctly from the previous action, resulting in [2]

Expected final state is an empty array, but since the state is not updated the final state looks detached from the actions taken to get there, therefore the "weird" value changes. This is how I've solved it in my code, using a RHF controller.

<Controller name={name} control={control} defaultValue={value} render={({ field: { onChange: internalOnChange, value } }) => ( <DropDownPicker searchable multiple={multiple} open={open} value={value} items={options} setOpen={setOpen} setValue={callback => { const newValue = callback(value); console.log('value', value, 'newValue', newValue); }} onSelectItem={val => { internalOnChange(val.map((v: ItemType<any>) => v.value)); }} /> )} />

Note that setValue doesn't do anything but log currently.

@PradeepThite
Copy link

@HugPet @mersiades
Can you assign this issue to me.

@PradeepThite
Copy link

I have also experienced same issue in multiple select

@mersiades
Copy link
Author

Can you assign this issue to me.

I don't think I have authority to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants