react-native - React-Native componentWillRecieveProps 渲染问题
问题描述
在我的 RN 项目中,我想实现这一点。
// componentWillReceiveProps componentWillReceiveProps = async (nextProps) => { let { searchText, peopleTab } = this.props;
let params = {};
if (peopleTab !== nextProps.peopleTab) {
params.peopleTab = nextProps.peopleTab;
}
// here i want to pass nextProps.searchText without a condition with the params like this.
// params.searchText = nextProps.searchText
if (Object.keys(params).length > 0) {
await this.props.fetchUnfollowedPeople(params);
}
}
如果有新值,我想发送带有 params 对象的 nextProps.searchText。否则我想用 params 对象发送 this.props.searchText 。
上面的代码,如果我取消注释
// params.searchText = nextProps.searchText
它给出了无限循环。我怎样才能做到这一点?
解决方案
设置let { searchText, peopleTab } = this.props;
incomponentWillReceiveProps
会导致粘贴新值
componentWillMount() {
this.searchText = this.props.searchText ;
this.peopleTab = this.props.peopleTab ;
}
componentWillReceiveProps = async (nextProps) => {
const params = [];
if (this.peopleTab !== nextProps.peopleTab) {
params['peopleTab'] = nextProps.peopleTab ;
}
if (Object.keys(params).length > 0) {
await this.props.fetchUnfollowedPeople(params);
}
}
推荐阅读
- python - 损失:使用 Keras 的 nan 与使用 tensorflow 的非 nan(工作)输出
- laravel-5.6 - Antonio Ribeiro 的 Tracker 中没有登录用户的数据
- swift - Swift 4 将字符串更改为 URL
- firebase - 如何更改 Firebase 项目的支持电子邮件?
- sed - SED - 用字符串换行变量替换字符串换行符
- django - pass parameter to form from template in Django 2
- angular - Angular 6: TypeError: Cannot read property 'invalid' of undefined?
- c - 从数组和单独结构计算平均值
- python - BeautifulSoup 找不到包含内容的元标记
- powershell - 更改 CSV 中的电子邮件地址