I'm getting data from API (using Redux Saga) and I want to show the data in a FlatList. In ComponentDidMount I'm dispatching the action to hit the API. Data are getting without any issue. But for the first time , data is not showing in the flatlist. If I enter Ctrl + S (Save) in VS code data is showing. I want to have set
class MovieDetailsScreen extends Component {
constructor(props) {
super(props);
this.state = {
data:[],
};
}
componentDidMount(){
this.props.getMovieData();
if(this.props.movieList){
this.setState({data:movieList})
}
}
renderItem = ({ item }) => {
return(
<View style={styles.mainContainer}>
<View style={styles.textContainer}>
<Text >{item.movieName}</Text>
<Text >{item.movieDescription}</Text>
</View>
)
}
render() {
return (
<View >
<FlatList
keyExtractor={(item, index) => index.toString()}
data={this.state.data}
renderItem={this.renderItem}
/>
</View>
);
}
}
function mapStateToProps(state) {
return {
movieList : state.movieListReducer.movieList
};
}
export function mapDispatchToProps(dispatch) { return{
getMovieData: () => dispatch(movieListActions.getMovieData()),
}
}
export default connect( mapStateToProps, mapDispatchToProps )(MovieDetailsScreen);
I tried adding a loader in flatlist until the data is getting loaded , but it's not working. Please note that I need to use setState({data:movieList}) because in future I have more implementations , so I couldn't use this.props.movieList in flatlist data
extraData={this.state.data}. Read more about that prop at reactnative.dev/docs/flatlist#extradata