Fetching different data for two drop-down menus- Only fetches data from one entity instead of two
import React, { Component } from 'react' import { ScrollView, Text, Picker, KeyboardAvoidingView } from 'react-native' import { connect } from 'react-redux' // Add Actions - replace 'Your' with whatever your reducer is called :) // import YourActions from '../Redux/YourRedux'
// Styles import styles from './Styles/FoodIngredientsScreenStyle' import MealPreparationPlaceActions from '../Redux/MealPreparationPlaceRedux' import MealTypeActions from '../Redux/MealTypeRedux' import { Actions as NavigationActions } from 'react-native-router-flux' // eslint-disable-line
// For empty lists import AlertMessage from '../Components/AlertMessage'
class FoodIngredientsScreen extends Component {
state = { page: 0, sort: 'id,asc', size: 20, loading: true, done: false, dataObjects: [] }
renderRow ({item}) {
return (
<TouchableOpacity onPress={NavigationActions.mealTypeEntityDetail.bind(this, { entityId: item.id })}>
renderEmpty = () =>
// renderSeparator = () =>
//
// The default function if no Key is provided is index // an identifiable key is important if you plan on // item reordering. Otherwise index is fine keyExtractor = (item, index) => index
// How many items should be kept im memory as we scroll? oneScreensWorth = 20
fetchMealTypes = () => { this.props.getAllMealTypes({ page: this.state.page, sort: this.state.sort, size: this.state.size }) };
fetchMealPreparationPlaces = () => { this.props.getAllMealPreparationPlaces({ page: this.state.page, sort: this.state.sort, size: this.state.size }) }
handleLoadMore = () => { if (this.state.done || this.props.fetching) { return } this.setState({ page: this.state.page + 1, loading: true }) this.fetchMealTypes(); this.fetchMealPreparationPlaces() }
componentWillReceiveProps (newProps) { if (newProps.mealPreparationPlaces) { this.setState({ done: newProps.mealPreparationPlaces.length < this.state.size, dataObjects: this.state.loading ? [...this.state.dataObjects, ...newProps.mealPreparationPlaces] : newProps.mealPreparationPlaces, loading: false }) }
}
componentWillMount () { this.fetchMealTypes() this.fetchMealPreparationPlaces()
}
render () {
return (
<Picker style={styles.picker}
itemStyle={styles.items}
selectedValue={this.state.name}
onValueChange={(name) => this.setState({name:name})}>
{this.state.dataObjects.map((mealType, key) => (
<Picker.Item label={"Meal Type: " + mealType.name} value={mealType.name} key={key}
/> )
)}
</Picker>
<Picker style={styles.picker}
itemStyle={styles.items}
selectedValue={this.state.name}
onValueChange={(name) => this.setState({name:name})}>
{this.state.dataObjects.map((mealPreparationPlace, key) => (
<Picker.Item label={"Meal Preparation: " + mealPreparationPlace.name} value={mealPreparationPlace.name} key={key}
/> )
)}
</Picker>
</ScrollView>
)
} }
const mapStateToProps = (state) => { return { // ...redux state to props here
mealTypes: state.mealTypes.mealTypes,
fetching: state.mealTypes.fetchingAll,
error: state.mealTypes.errorAll,
mealPreparationPlaces: state.mealPreparationPlaces.mealPreparationPlaces,
fetching: state.mealPreparationPlaces.fetchingAll,
error: state.mealPreparationPlaces.errorAll
} }
const mapDispatchToProps = (dispatch) => { return {
getAllMealTypes: (options) => dispatch(MealTypeActions.mealTypeAllRequest(options)),
getAllMealPreparationPlaces: (options) => dispatch(MealPreparationPlaceActions.mealPreparationPlaceAllRequest(options))
} }
export default connect(mapStateToProps, mapDispatchToProps)(FoodIngredientsScreen)
This sort of generic React + Redux question is better suited for Stack Overflow or the Reactiflux Discord chat.
I suggest creating a Code Sandbox that demonstrates the problem you're seeing before posting in either of those groups though. It will make it easier for people to look and provide help!
Thank you . My problem is not with the entire code. It is with componentWillReceiveProps i want it to be able to fetch data from two different entities which have different values
The values fetched should be displayed on two dropdown menus do you think you could assist with that ?