React-History.replace在try / catch中创建错误

问题描述 投票:0回答:1

早上好

当我验证表单时,我有一个handleSubmit函数,一切正常,如果有任何问题,我的api会向我发送错误,并且仅完成注册,如果一切正常,则无法重定向... History.replace系统地创建一个我不理解的错误,尤其是因为我在网站上所有位置的所有.replace都正常工作时。

我的handleSubmit函数:

    const handleSubmit = async e => {
        e.preventDefault();
        const apiErrors = {};
        if(user.password !== user.passwordConfirm) {
            apiErrors.passwordConfirm = "mdpError"
            setErrors(apiErrors)
            return;
        }
        try {
            await usersApi.register(user)
            setErrors({})
            history.replace("/")
        } catch (error) {
            const { violations } = error.response.data;
            if (violations) {
                violations.forEach(violation => {
                    apiErrors[violation.propertyPath] = violation.message
                })
                setErrors(apiErrors);
            }
        }
    }

控制台显示此错误:未捕获(承诺中)TypeError:无法读取未定义的属性'data'

我的{history}组件的参数恢复得很好...

如果有人有主意,非常感谢!

reactjs try-catch react-hooks browser-history
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.