当我在componentDidMount中使用async时,Component将一次又一次地挂载和卸载。为什么?

时间:2019-04-16 14:13:36

标签: reactjs async-await next.js

当我调用getCityName时,除非我删除了async,否则组件将一次又一次卸载和DidMount。所有代码都在nextjs中运行。

     this.state = {
                bank_account: {
                    // bank_name: '',
                    // number: '',
                    // city: '',
                    // branch_name: ''
                },

                allCity: []
            };   

     componentDidMount() {
            const { owner_cellphone } = this.props;
            this.getDraft(owner_cellphone);
            this.fetchCity();
        }


        fetchCity = async () => {
            const { data, error } = await getCity();
            if (error) {
                return;
            }
            console.log(data);
            this.setState({ allCity: data });
        };


        getCityName = cityString => {
            const { allCity } = this.state;
            console.log(allCity);

            if (!allCity || !cityString) {
                return;
            }
            const cityArray = cityString.split(' ');
            console.log(cityArray);
            const targetProvince = allCity.find(item => item.code === cityArray[0]);
            const targetCity = targetProvince.children.find(item => item.code === cityArray[0]);
            return targetProvince.name + targetCity.name;
        };

        render() {   
            const {  bank_account } = this.state;
         
            const cityValue = this.getCityName(bank_account.city);
            return (
             <Item label="开户城市" icon={<Icon type="arrow-right" />} onClick={this.showCitySelect}>
                            <input
                                className="item-picker-input"
                                value={cityValue}
                                  />
                        </Item>

            );
                   
    }

2 个答案:

答案 0 :(得分:0)

之所以不起作用,是因为您正在从同步功能中调用异步功能。 我不确定是否可以,但是您可以尝试。.

getCityName = async (cityString) => {
        const { allCity } = this.state;
        console.log(allCity);

        if (!allCity || !cityString) {
            return;
        }
        const cityArray = cityString.split(' ');
        console.log(cityArray);
        const targetProvince = allCity.find(item => item.code === cityArray[0]);
        const targetCity = targetProvince.children.find(item => item.code === cityArray[0]);
        return targetProvince.name + targetCity.name;
};  
render = async () => {   
        const {  bank_account } = this.state;

        const cityValue = await this.getCityName(bank_account.city);
        return (
         <Item label="开户城市" icon={<Icon type="arrow-right" />} onClick={this.showCitySelect}>
                        <input
                            className="item-picker-input"
                            value={cityValue}
                              />
                    </Item>

        );

}

答案 1 :(得分:0)

    getCityName = cityString => {
        const { allCity } = this.state;
        if (allCity === [] || !cityString) {
            return;
        }
        const cityArray = cityString.split(' ');
        let targetProvince = allCity.find(item => item.code === cityArray[0]);
        if (targetProvince) {
            let newProvince = JSON.parse(JSON.stringify(targetProvince));
            const targetCity = newProvince.children.find(item => item.code === cityArray[1]);
            return `${targetProvince.name} ${targetCity.name}`;
        }
        return '';
    };

我认为这可能是深度复制的问题。