【发布时间】:2018-06-05 15:17:26
【问题描述】:
我正在使用 Udemy: The Complete React Native and Redux Course by Stephen Grider 学习 RN,并且正在使用 Firebase 创建一个管理应用程序。
我有来自react-redux 库的连接函数和mapStateToProps(),所以每次我的状态发生变化时,我都会在组件中接收它们作为道具。
我创建了一个从 Firebase 数据库获取数据的操作,我将在 componentWillMount() 中调用它,但由于获取数据是一项异步任务,我必须在 componentWillReceiveProps() 中创建我的数据源。
但是教练说我必须在componentWillMount() 和componentWillReceiveProps() 中都给我的createDataSource() 打电话
.
我不明白为什么!如果我的状态有任何变化(这是我的员工名单),我会将它们作为道具接收,所以我认为只需在 componentWillReceiveProps() 中调用 createDataSource() 就足够了。
有人可以为我声明吗?有没有我忘记处理的特殊情况?
更新
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);
}
所以我使用 ListView 来展示我从 Firebase 获取的员工!如果我只在componentWillReceiveProps() 中使用createDataSource() 会有什么问题吗?
【问题讨论】:
-
你能发布一些代码吗?很难理解你的意思。通过阅读你,我会说 A)Reducers 不应该获取数据 B)componentWillReceiveProps(我认为它已被弃用)听起来像你应该使用的方法 C)尝试尽可能多地使用功能组件与 Redux 和 D)用户 redux -thunk 处理异步操作
-
@PabloBarriaUrenda 代码已添加。 A) 是一个错字,我的意思是操作 B) 那么还有其他方法吗?
-
E) 什么是 createDataSource?这是 Firebase 特有的东西吗?
-
不幸的是,Stephen Grider 在 Udemy 的课程已经过时,以至于现在已经没用了。而且作者已经一年多没有回复用户更新课程的问题了。他使用的许多材料、API、方法、工具在 2019 年现在已经无关紧要了。但我同意,它们在 2017 年和 2018 年初都非常好
标签: javascript reactjs react-native redux react-redux