this.setState({})的结果未定义

时间:2019-05-21 17:41:47

标签: javascript reactjs react-native

...

componentWillMount() {
    this.functionA();
}


functionA = () => {
    this.animated = new Animated.Value(0);

    this.panResponder = PanResponder.create({
        ...

        onPanResponderRelease: (e, gestureState) => {
            const { dy } = gestureState;

            if(dy < -100) { // Swipe up away
                Animated.timing(this.animated, {
                    toValue: -800,
                    duration: 350
                }).start(async () => {
                    this.props.hideScoreModal();
                    if(...) {
                        await this.functionB();
                        await this.functionC();
                    }   
                });

            } ...
        }
    })
}

functionB = () => {
    ... // do certain calculations

    const new_scoreData = {
        ... // insert calculated data here
    };

    this.setState({
        new_scoreData: new_scoreData
    });
}

functionC = () => {
    // Send this.state.new_scoreData to DB
    console.warn('new score data = ' + this.state.new_scoreData)
}
...

functionC()控制台中显示新分数数据=未定义

如果我在控制台中添加this.state = { new_scoreData : {} },我会看到新分数数据= {}

因此,this.setState({})似乎没有完成任务。

如果转到console.warn(new_scoreData)中的functionB(),我可以看到正确的内容。

请帮助我了解为什么this.setState({})在我的情况下无法正常工作。

0 个答案:

没有答案