在我的RN项目中,我想要实现这一目标。
// componentWillReceiveProps componentWillReceiveProps =异步(nextProps)=> { 让{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
给出无限循环。我该如何实现?
答案 0 :(得分:0)
在let { searchText, peopleTab } = this.props;
中设置componentWillReceiveProps
会导致粘贴新值
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);
}
}