I'm learning RN with Udemy: The Complete React Native and Redux Course by Stephen Grider and i'm creating a managing app with Firebase.
I have my connect function from react-redux library and have mapStateToProps() so every time i have changes in my states, i will receive them as props in my component.
I created an action to fetch data from Firebase database and i'm going to call it in the componentWillMount() but since fetching data is an async task, i have to create my data source in the componentWillReceiveProps().
But instructor said i have to call my createDataSource() in both componentWillMount() and componentWillReceiveProps()
.
I can't understand why!! if i have any changes in the states (which here is my employees list), i will receive them as props, so i think it's enough to call createDataSource() in componentWillReceiveProps() only.
Can anyone declare that for me please? Is there any special case that i'm forgetting to handle?
UPDATE
EmployeeActions.js:
export const employeesFetch = () => {
const { currentUser } = firebase.auth();
return dispatch => {
firebase
.database()
.ref(`/users/${currentUser.uid}/employees`)
.on("value", snapshot => {
dispatch({ type: EMPLOYEES_FETCH_SUCCESS, payload: snapshot.val() });
});
};
};
EmployeeList.js:
componentWillMount() {
this.props.employeesFetch();
this.createDataSource(this.props);
}
componentWillReceiveProps(nextProps) {
this.createDataSource(nextProps);
}
createDataSource({ employees }) {
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.dataSource = ds.cloneWithRows(employees);
}
so i'm using ListView to show my fetched employees from Firebase! Will i have any problem if i just use createDataSource() in the componentWillReceiveProps()?