React-Native componentWillRecieveProps渲染问题

时间:2018-09-10 09:13:38

标签: react-native

在我的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

给出无限循环。我该如何实现?

1 个答案:

答案 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);
    }
}