reactjs - 如何使用 componentWillRecieveProps 在 React Native 中重新渲染 Flatlist?
问题描述
UNSAFE_componentWillMount(){
this.props.EmployeeFetch();
}
renderRow(employee){
return <ListItem employee={employee}/>;
}
render(){
return(
<FlatList style={{flex:1,height:100}}
data = {this.props.employees}
/>
);
}
}
const mapStateToProps=state=>{
const employees = _.map(state.employees,(val,uid)=>{
return {...val,uid};
});
return {employees};
}
export default connect(mapStateToProps, {EmployeeFetch})(EmployeeList);
在这里,我正在从 firebase 获取数据。起初它是空的,过了一段时间数据来了。那么,我将如何使用 Flatlist 和 componentWillRecieveProps() 重新渲染新数据?
解决方案
所以componentWillReceiveProps
不推荐使用,你应该使用getDerivedStateFromProps
.
您可以在示例中像这样使用它:
static getDerivedStateFromProps(props, state) {
const { employees } = this.props;
return employees;
}
然后在你的render
:
render() {
const { employees } = this.props;
// then use map(employees)
}
推荐阅读
- android - 从 greendao 迁移到 Android Room
- kendo-ui - 如何根据数据在特定行上的 KendoUI 网格中显示编辑按钮
- c# - 如何在运行时更改统一照明环境颜色强度?
- java - 不止一次将枚举对象转换为列表
- python - 如何使用 netCDF4 模块仅读取指定时间段的数据?
- python - 如何在 windows (Anaconda) 中安装 NLTK 数据
- linux - 删除 QDialogs 默认关闭按钮
- r - 两个列表之间的部分 str_match,边界问题
- wordpress - 附加了其他内容的 Wordpress 页面 slug
- ios - 如何像 3D Square 一样设置 UIScrollView