首页 > 解决方案 > 未调用 componentDidUpdate

问题描述

我有一个反应本机应用程序,我在 App.js 上调用 componentDidUpdate,但它没有触发。我想知道这是否是因为我是从 App.js 调用的?这是 App.js 文件:

class App extends Component {

    componentDidUpdate = () => {
        if (this.props.text && this.props.text.toString().trim()) {
            Alert.alert(this.props.title || 'Mensagem', this.props.text.toString());
            this.props.clearMessage();
        }
    }

    render() {
        return (
            <NavigationContainer>
                <Navigator />
            </NavigationContainer>
        )
    }
}

const mapStateToProps = ({ message }) => {
    return {
        title: message.title,
        text: message.text
    }
}

const mapDispatchToProps = dispatch => {
    return {
        clearMessage: () => dispatch(setMessage({
            title: '',
            text: ''
        }))
    }
}

const connectDispatch = connect(mapStateToProps, mapDispatchToProps);
const connectApp = connectDispatch(App);

export default connectApp;

这就是我所说的。在帖子操作中的调度中。

    .then(res => {
        dispatch(fetchPosts());
        dispatch(postCreated());
        dispatch(setMessage({
            title: 'Sucesso',
            text: 'Nova Postagem!'
        }));
    });

所有其他调度都被解雇。阻止警报被触发的不是 if,因为我已经将警报放在 if 之外。

标签: react-nativeredux

解决方案


改变这个

componentDidUpdate = () => { ... }

为了这:

componentDidUpdate(prevProps, prevState, snapshot) { ... }

请记住 componentDidUpdate 不会在第一次渲染时触发


推荐阅读