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

PickerIOS selectedValue reset when change value #25352

Closed
AlexiaTorres opened this issue Jun 21, 2019 · 3 comments
Closed

PickerIOS selectedValue reset when change value #25352

AlexiaTorres opened this issue Jun 21, 2019 · 3 comments
Labels
Bug Component: PickerIOS Platform: iOS iOS applications. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@AlexiaTorres
Copy link

When I change selected option, it resets to first.
For example if this.props.center is C and i have:
A
B
C <-
D
E

I start with C selected, i change to E for example, state on console.log is E but it shows A selected.

React Native version:
0.57.1

Here is my code:

constructor(props) {
        super(props);
        this.state = {
            selectedCenter: this.props.center
        };
    }

    render() {
        if (this.props.centers) {
            return (
                <View style={{backgroundColor: '#008590d9'}}>
                    <PickerIOS
                        selectedValue={this.state.selectedCenter}
                        style={styles.select}
                        onValueChange={(itemValue, itemIndex) => {
                            console.log(itemValue);
                            this.setState({selectedCenter: itemValue});
                        }}>
                        {
                            this.props.centers.map((center) => {
                                console.log(this.state.selectedCenter);
                                return <PickerIOS.Item key={center.id} title={center.name} label={center.name}
                                                       value={center}/>
                            })
                        }
                    </PickerIOS>
                </View>
            )
        }
    }
@ltphan
Copy link

ltphan commented Jun 24, 2019

I would take a look at this screenshot for why it would be set initially to A instead of the value you wanted:
Screen Shot 2019-06-24 at 9 39 59 AM

This is the link to the problem you could possibly be having:
https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html

I'm not an expert in this as I am just learning React-Native myself, you have seem to initialized a state to a prop input and to update it on onValueChange for the selected centre. However PickerIOS accepts multiple props from the documentation and we can see that if any of those props change, the component itself will re-render, which will lose the value you wanted to set it to.

I hope that is clear!

@stale
Copy link

stale bot commented Sep 22, 2019

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Sep 22, 2019
@stale
Copy link

stale bot commented Sep 29, 2019

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Sep 29, 2019
@facebook facebook locked as resolved and limited conversation to collaborators Sep 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Component: PickerIOS Platform: iOS iOS applications. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

3 participants