【问题标题】:React Router v4 Redirecting on form submitReact Router v4 在表单提交时重定向
【发布时间】:2018-09-19 03:57:58
【问题描述】:

全新的反应,点击“登录”页面上的提交后尝试重定向到“主页”。尝试按照反应路由器教程进行操作。

当我单击表单上的提交按钮和控制台记录状态和 fakeAuth.isAuthenticated 时,它们都返回 true。但是,重定向没有触发。

Login.js

class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            portalId: "",
            password: "",
            redirectToReferrer: false
        }

        this.handleSubmit = this.handleSubmit.bind(this);
    }


    handleSubmit(e) {
        fakeAuth.authenticate(() => {
            this.setState({
                portalId: this.refs.portalId.value,
                password: this.refs.password.value,
                redirectToReferrer: true
            })
        })
        e.preventDefault();
    }


    render() {
        const redirectToReferrer = this.state.redirectToReferrer;
        if (redirectToReferrer === true) {
            <Redirect to="/home" />
        }

Routes.js

export const fakeAuth = {
    isAuthenticated: false,
    authenticate(cb) {
        this.isAuthenticated = true
        setTimeout(cb, 100)
    },
    signout(cb) {
        this.isAuthenticated = false
        setTimeout(cb, 100)
    }
}

const PrivateRoute = ({ component: Component, ...rest }) => (
    <Route {...rest} render={() => (
        fakeAuth.isAuthenticated === true
        ? <Component {...this.props}/>
        : <Redirect to="/" />
    )}/> 
)


export default () => (
    <BrowserRouter>
        <div>
            <Navbar />
            <Switch>
                <Route path="/" exact component={Login} />
                <Route path="/register" exact component={Register} />
                <Route path="/home" exact component={Home} />
            </Switch>
        </div>
    </BrowserRouter>
);

【问题讨论】:

    标签: reactjs react-router-v4


    【解决方案1】:

    对于使用 Hooks 的人,请看这里 -> https://reactrouter.com/web/api/Hooks/usehistory 使用示例:

    import { useHistory } from "react-router-dom";
    let history = useHistory();
            
    const onSubmit = () => {
        //code
        history.push(`/${link}/${object.id}`)
    }
    

    【讨论】:

    • 谢谢,这也适用于我。
    【解决方案2】:

    当我们在路由器声明之外使用时。你可以使用

    this.props.history.push('/url')

    在你的函数调用中。应该与 react-router-dom 一起使用

    【讨论】:

      【解决方案3】:

      你必须在渲染方法中返回Redirect(否则它不会被渲染,因此重定向不会发生):

        render() {
              const redirectToReferrer = this.state.redirectToReferrer;
              if (redirectToReferrer) {
                  return <Redirect to="/home" />
              }
              // ... rest of render method code
         }
      

      【讨论】:

      • 谢谢,这成功了。应该早点发布,而不是在这上面浪费很多时间。
      • @VincentNguyen 不客气。如果对您有帮助,请点赞并接受答案:)
      【解决方案4】:

      我现在可以通过使用withRouter&lt;Redirect to="/home" /&gt; 换成this.props.history.push("/home"); 来重定向到“主页”。不知道为什么第一种方法行不通。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多