【问题标题】:Redirect doesn't work in React Router重定向在 React Router 中不起作用
【发布时间】:2017-10-05 14:42:09
【问题描述】:

所以我试图在主页上使用登录表单制作简单的应用程序,然后重定向到仪表板。尝试将 /dashboard 页面设为私有时遇到问题。代码如下:

import React, { Component } from 'react';
import { connect } from 'react-redux';
import {Redirect} from 'react-router-dom'

class DashBoard extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        if (this.props.auth.token) {
            return (
                <h2>Here will be dashboard with items.</h2>
            );
        } else {
            return <Redirect to={{pathname: '/'}} push/>
        }
    }
}

export default connect(
    (state) => {
        return state;
    }
)(DashBoard);

问题是 url 改变了,但组件并没有真正呈现自己。那么为什么在仪表板中重定向不起作用?

编辑:我终于设法从 Home 组件进行重定向,但对仪表板做同样的事情仍然不起作用!

import React, {Component} from 'react';
import {connect} from 'react-redux';
import * as actions from 'actions';
import {Redirect} from 'react-router-dom';

class Home extends Component {
    constructor(props) {
        super(props);
        this.handleSubmit = this.handleSubmit.bind(this);
    }
    componentWillReceiveProps(nextProps) {
        console.log('nextProps');
        if (!nextProps.isLoading) {
            if (nextProps.auth.error) {
                console.log('error');
            } else if (nextProps.auth.token) {
                console.log('success');
            } else {
                console.log('something else');
            }
        }
    }

    handleSubmit(e) {
        let {isLoading, auth, dispatch} = this.props
        e.preventDefault();
        let email = this.refs.email.value;
        let password = this.refs.password.value;
        dispatch(actions.login(email, password))
    }
    render() {
        let {isLoading, auth} = this.props;
        let renderLoading = () => {
            if (isLoading) {
                return 'Loading...'
            } else {
                return 'Submit';
            }
        }
        let renderMessage = () => {
            if (auth.error) {
                return <p className="error-message">{auth.error}</p>;
            } else if (auth.token) {
                return <p className="success-message">You have successfully logined in.</p>
            } else {
                return <p></p>
            }
        }
        if (auth.token) {
            return (<Redirect to='/dashboard'/>)
        }
        return (
            <div className="form-container">
                {renderMessage()}
                <form onSubmit={this.handleSubmit}>
                    <div className="field">
                        <label>First Name</label>
                        <input type="text" name="email" placeholder="First Name" ref="email" />
                    </div>
                    <div className="field">
                        <label>Password</label>
                        <input type="text" name="password" placeholder="Last Name" ref="password"/>
                    </div>
                    <button className="form-button" type="submit">{renderLoading()}</button>
                </form>
            </div>
        );
    }
}

export default connect(
    (state) => {
        return state;
    }
)(Home);

【问题讨论】:

    标签: reactjs redirect redux react-router


    【解决方案1】:

    更换浏览器 确保您的浏览器接受 cookie

    【讨论】:

    • 欢迎来到 StackOverflow!此答案不提供满足 OP 需求的解决方案,请仅在您确定解释有效答案时才回答
    【解决方案2】:

    您是否阅读过位于here 的 Redux 集成指南?奇怪的是 Redux 正在实现 shouldComponentUpdate 并阻止您的组件被渲染。这是一个你可以使用的技巧,

    import React, { Component } from 'react';
    import { connect } from 'react-redux';
    import {Redirect, withRouter} from 'react-router-dom'
    
    class DashBoard extends Component {
        constructor(props) {
            super(props);
        }
        render() {
            if (this.props.auth.token) {
                return (
                    <h2>Here will be dashboard with items.</h2>
                );
            } else {
                return <Redirect to={{pathname: '/'}} push/>
            }
        }
    }
    
    export default withRouter(connect(
        (state) => {
            return state;
        }
    )(DashBoard));
    

    【讨论】:

    • 这对我不起作用。我只是发现重定向后,如果我查看反应开发工具并打开路由器上下文,我会看到该位置是 /dashboard,但在浏览器中它显示我 localhost:3001。可能这是问题所在?但是为什么 Home 组件中的代码实际上是一样的呢?
    • 你试过在 react-redux 的 connect 函数中使用 pure: false 吗?你可以试试这个方法:connect(null, null, null, {pure: false})(Component)API 阐明了此选项在 true 时的作用。
    • @AndreyLuiz,不幸的是这没有用。有什么建议为什么在主页组件上重定向有效而在仪表板上无效?
    • Home 连接到 redux 了吗?它使用连接功能吗?
    • @AndreyLuiz,是的。我不知道到底发生了什么,但一切正常。我没有注意到我做了什么的一个问题,因为我只是在我的项目上努力。这很奇怪,需要 git diff 那个。
    猜你喜欢
    • 2021-07-27
    • 2021-07-24
    • 2018-05-21
    • 2021-02-11
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    相关资源
    最近更新 更多