【问题标题】:How to redirect user after submitting form with React使用 React 提交表单后如何重定向用户
【发布时间】:2019-10-24 22:14:11
【问题描述】:

我正在学习 React。我有一个带有表单(“/ login”)的登录页面,在成功提交后我想将用户重定向到域的根路径(“/”)。我正在使用似乎工作正常的 Redux。我的代码是:

const handleSubmit = (e) => {

        e.preventDefault()

        setSubmitBtn(true)

        setTimeout(async (e) => {

            const { data } = await axios.post( axiosURL + '/login', inputs)

            dispatch({ type: 'LOGIN' })

            history.push("/")

            setReturnMessage(data.message)

            setSubmitBtn(false)

        }, 1000)
    }

问题是 history.push("/") 不起作用,它给出了以下错误消息:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
    in Login (created by Context.Consumer)
    in Route (at routes.js:36)

有人知道怎么解决吗?谢谢!!

【问题讨论】:

    标签: javascript reactjs redux react-redux react-router


    【解决方案1】:

    问题是你在history.push("/")之后这样做:

    setReturnMessage(data.message) 
    setSubmitBtn(false)
    

    评论这些行,错误应该会消失。

    你也可以这样做:

    setReturnMessage(data.message)
    setSubmitBtn(false)
    history.push("/")  <--- as the last line of code
    

    【讨论】:

      猜你喜欢
      • 2020-03-06
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 2020-09-12
      • 2017-04-03
      • 1970-01-01
      相关资源
      最近更新 更多