Skip to content

Picker - fix not responding to external value change when renderPicker is used #1211

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

Merged

Conversation

M-i-k-e-l
Copy link
Collaborator

@M-i-k-e-l M-i-k-e-l commented Mar 4, 2021

Description

Picker - fix not responding to external value change when renderPicker is used.
This does not solve this issue when using useNativePicker, that requires a bigger change.

Can be tested with the following PlaygroundScreen:

import _ from 'lodash';
import React, {Component} from 'react';
import {StyleSheet} from 'react-native';
import {View, Text, Card, TextField, Button, Picker, PickerItemValue} from 'react-native-ui-lib'; //eslint-disable-line

export default class PlaygroundScreen extends Component {
  render() {
    return (
      <View bg-dark80 flex padding-20 center>
        <View marginT-20>
          <TextField placeholder="Placeholder" />
        </View>
        <Card height={100} center padding-20>
          <Text text50>Playground Screen</Text>
        </Card>
        <View flex center>
          <Button marginV-20 label="Button"/>
        </View>
        <PickerBug/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {},
});

class PickerBug extends React.Component {
  state = {
    val: 1
  };

  getNumberPickerRenderer = (label: string | undefined) => (value?: PickerItemValue) => (
    <View padding-20 row>
      <View marginR-s5 flex>
        <Text body>{label}</Text>
      </View>
      <View>
        <Text useCustomTheme bodyBold>
          {value ?? null}
        </Text>
      </View>
    </View>
  );

  numberOptions = [
    {label: '1', value: 1},
    {label: '2', value: 2},
    {label: '3', value: 3}
  ];

  render() {
    return (
      <>
        <Text>Real value: {this.state.val}</Text>
        <Button link onPress={() => this.setState({val: this.state.val + 1})} label="+"/>
        <Button link onPress={() => this.setState({val: this.state.val - 1})} label="-"/>
        <Picker
          migrate
          value={this.state.val}
          onChange={(val: number) => this.setState({val})}
          renderPicker={this.getNumberPickerRenderer('label')}
        >
          {this.numberOptions.map(item => (
            <Picker.Item key={item.value} value={item.value} label={item.label}/>
          ))}
        </Picker>
      </>
    );
  }
}

Changelog

Picker - fix not responding to external value change when renderPicker is used (without useNativePicker)

@@ -186,6 +186,10 @@ class Picker extends Component {
value: nextProps.value
};
}
} else if (_.isFunction(nextProps.renderPicker) && prevState.value !== nextProps.value) {
return {
value: nextProps.value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to update prevValue as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, not sure what prevValue is used for, it seems to reside only in the previous if statement.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We store prevValue on state for the getDerivedStateFromProps comparison. I think it has to be updated every time we update value otherwise the condition won't be up-to-date...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, updated

…alue-change

* master:
  Fix Badge's size default prop (#1212)
  Fix RadioGroup typings to extend our ViewProps and not React Native's
@Inbal-Tish Inbal-Tish merged commit d63f0e9 into master Mar 9, 2021
@M-i-k-e-l M-i-k-e-l deleted the fix/picker-with-render-picker-respond-to-value-change branch October 4, 2021 08:26
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

Successfully merging this pull request may close these issues.

2 participants