erksch / react-native-wheely

An all JavaScript wheel picker for react-native without any native code.
413 stars 59 forks source link

When I use setState to set the options property, However, the old values(by options control) in the view are not updated. #27

Closed erksch closed 2 years ago

erksch commented 2 years ago

When I use setState to set the options property, However, the old values(by options control) in the view are not updated.

Originally posted by @daxiangfei in https://github.com/erksch/react-native-wheely/issues/20#issuecomment-1091372488

erksch commented 2 years ago

@daxiangfei As described in the memoization section in README, the options have to be fixed on the first render. You probably can wrap the picker in a component that renders when the options are available.

kuzkokov commented 5 months ago

btw, there is a simpler solution — just use the key prop. like this f.e.:

const optionLabels = options.map(option => option.label);
return (
  <WheelPicker
    containerStyle={styles.picker}
    decelerationRate="normal"
    itemHeight={40}
    itemTextStyle={styles.pickerItemText}
    key={optionLabels.join('')}
    options={optionLabels}
    selectedIndex={selectedIndexes[index]}
    selectedIndicatorStyle={styles.selectedIndicator}
    visibleRest={2}
    onChange={selected => handlePickerChange(index, selected)}
  />