chymtt / ReactNativeDropdownAndroid

A simple wrapper for Android's Spinner in react-native
MIT License
62 stars 22 forks source link

How to set the default value before any selection? #6

Closed ACCTFORGH closed 8 years ago

ACCTFORGH commented 8 years ago

It seems like the selected props is the one to use however my app crashes when I use it in the code. Thanks!

chymtt commented 8 years ago

It's true that the selected prop should be used. Could you please provide a log or your code snippet? I can't work it out just based on what you said.

On Sat, Dec 12, 2015 at 3:58 PM, ZJ notifications@github.com wrote:

It seems like the selected props is the one to use however my app crashes when I use it in the code Thanks!

— Reply to this email directly or view it on GitHub https://github.com/chymtt/ReactNativeDropdownAndroid/issues/6.

ACCTFORGH commented 8 years ago

I was playing with it for a while. My code snippet is as below

< Dropdown style={styles.materialDropdown} values={this.props.data.material_list} selected={1} onChange={ (data) => { this.setState({material:data.value}); }} />

this.props.data.material_list is data passed from its owner view. The owner retrieves the material_list from server and updates it at componentDidMount(). I think the error is because the network call is asynchronous so the component could be accessing undefined. I tried to change to code to below where I add a default value for this.state.material_list and setState when the network call finishes but the selected props does not work anymore. Any idea how I can implement this feature?

< Dropdown style={styles.materialDropdown} values={this.state.material_list} selected={1} onChange={ (data) => { this.setState({material:data.value}); }} />

chymtt commented 8 years ago

Thank you for pointing it out. It's a bug in my native code that make it refuse to update the "selected" value after re-rendering. I've fixed the issue and pushed it, please try it out to see if your problem persists.