javascript - 当Reducers更新状态时,路由重定向不会发生

标签 javascript reactjs react-native react-router reducers

我收到一个使用 reducer 的用户模型。第一个值是 null(因为这是Reducers 的行为方式——这意味着它正在等待并且尚未收到模型),第二个值是 User 模型本身。

当我获得第一个空值时,组件重定向到“IsNotSetUp”路径,但是当我获得第二个值时,组件将 isUserSetUp 属性显示为 TRUE,但它不会重定向到“IsSetUp”路径?这是怎么回事?

enter image description here

class App extends Component {
    state = { isUserSetUp: false }

    componentWillReceiveProps(nextProps) {
        if (nextProps.auth && nextProps.auth.isProfileSetUp) {
            this.setState({ isUserSetUp: true }, function() {
                console.log("This is when we receive the User model from the reducer");
            });
        }
        else {
            this.setState({ isUserSetUp: false });     
        }

    }

    componentDidMount() {
        this.props.fetchUser();
    }

    render() {
        return (
            <div className="container">
                <BrowserRouter>
                    <div>
                        <Header />
                        <Route exact path="/" component={Landing} />
                        <Route exact path="/dashboard" component={Dashboard} />

                        {/* Profile Set up */}
                        <Route exact path="/setup" render={() => 
                        (
                            this.state.isUserSetUp ? (
                                <Redirect to={'/IsSetUp'} />
                            ) : (
                                <Redirect to={'/IsNotSetUp'} />
                            )
                        )}
                     />
                    </div>
                </BrowserRouter>
            </div>
        );
    }
};

出了什么问题?有什么解决方法吗?我采取的更好的建议路径是什么?

提前致谢,美女;)

最佳答案

您可以使用hashHistory来实现这一点。

import {
  hashHistory
} from 'react-router';

/**
 * [update route of the application]
 * @param  {String} route [new route of the application]
 */
const updateRoute = (route) => {
  hashHistory.push(route);
};

updateRoute('/IsSetUp');
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react-dom.min.js"></script>

如果有帮助请告诉我。

关于javascript - 当Reducers更新状态时,路由重定向不会发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46722584/

相关文章:

javascript - 上传超过 100 KB 的文件时出现 500 内部服务器错误

javascript - 如何在 Facebook 的 FixedDataTable 中设置灵活的行高?

reactjs - 如何在 React Native 上获取设备的 ip?

react-native - React Navigation 5 标题重叠

javascript - Bootstrap日期选择器: Ensuring EndDate > StartDate as well as having a max start date

javascript - 使用 javascript(内部 html)传递到 html 时多重选择不起作用

javascript - 使用 jQuery 交换列

reactjs - componentWillMount 在渲染之前未完成

reactjs - 如何在 React JS 中处理具有不同路由的组件

react-native - 版本 1.5.1 nxworkspace + React Native 的 Axios url 未定义问题