1
ネイティブに反応します。 APIを使用してデータベースからピッカーに値を追加したいのですがどうすればよいのでしょうか。ここでPickerのAPIデータ値を加算する
componentDidMount() {
return fetch('https://reactnativecode.000webhostapp.com/FruitsList.php')
.then((response) => response.json())
.then((responseJson) => {
let ds = new Picker.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({
isLoading: false,
dataSource: ds.cloneWithRows(responseJson),
}, function() {
// In this block you can do something with new state.
});
})
.catch((error) => {
console.error(error);
});
}
は、私はAPI関数componentDidMount(からフェッチされた値)、ここで解決策を見つけた
render() {
if (this.state.isLoading) {
return (
<View style={{flex: 1, paddingTop: 20}}>
<ActivityIndicator />
</View>
);
}
return (
<View style={styles.MainContainer}>
<Picker
selectedValue={this.state.active}
dataSource={this.state.dataSource}
onValueChange={(activeValue, activeIndex) => this.setState({active:
activeValue})}>
renderRow={(rowData) => <Picker.Item label={rowData.fruit_name} value=
{rowData.fruit_name} />}
</Picker>
</View>
);}}