首页 > 解决方案 > componentDidMount 有趣的问题

问题描述

我有以下代码

  componentDidMount() {
        if (this.props && this.props.match && this.props.match.params.code) {
            console.dir("mount1");
            this._handleCode(this.props.match.params.code);
        } else {
            console.dir("mount2");
            this.setState({loading: false});
        }
    }

当 url 参数中有代码时,为什么先打印 mount2 然后再打印 mount1?前任。/subscriptionGroup/测试

 <ProtectedRoute path="/subscriptioncode/:code" component={SubscriptionCodeActivate}/>

const ProtectedRoute = ({
                                   path,
                                   component: Component,
                                   render,
                                   ...rest
                               }) => {
    return (
        <Route
            path={path}
            {...rest}
            render={props => {
                if (Utils.isAuthenticated()) {
                    return Component ? <Component {...props} /> : render(props);
                } else {
                    return <Redirect to={`/login?redirect=${props.location.pathname}`} />;
                }
            }}
        />
    );
};

标签: reactjs

解决方案


推荐阅读