reactjs - react-redux-firebase 登录承诺在更新身份验证状态之前解决,导致渲染问题
问题描述
我正在使用 redux-form、react-redux-firebase 以及 react-router-redux 和 react-router。
当使用 redux-form 连接表单登录用户时,在 onSubmit 回调函数中,我使用的是来自withFirebase
HOC 的 react-redux-firebase 函数,称为props.firebase.login()
. 在这个承诺的回报中,我试图使用 react-router 导航到我的“受保护”视图。
这一切都很好,看起来像这样:
export default withFirebase(
connect(mapStateToProps, mapDispatchToProps)(
reduxForm({
form: SINGLE_PAGE_FORMS.login,
onSubmit: (values, dispatch, props) => {
const { firebase } = props;
const { username: email, password } = values;
firebase.login({
email,
password,
}).then(response => {
if(!!response) {
dispatch(
openNotificationCenterAC({ message: 'Successfully logged in!', messageType: MESSAGE_TYPE_SUCCESS })
);
// The problem here with login is that the Private Route has not yet seen the Uid update and therefore
// won't load a private route. I need to know the UID is in state before navigating to dashboard.
dispatch( push( ROUTE_OBJS.SECURED[ROUTES.DASHBOARD].path ) );
dispatch( reset(SINGLE_PAGE_FORMS.login) );
}
}).catch(error => {
dispatch(
openNotificationCenterAC({ message: error.message, messageType: MESSAGE_TYPE_ERROR })
);
});
},
})(LoginContainerView)
)
);
麻烦(如评论中所见)是我(今天)已经切换到使用一种非常酷的新方法将我的一些路线设为私有。我使用此处链接中的示例来创建此概念。
我的私有组件如下所示:
export default class PrivateRouteView extends Component {
render() {
const {
key,
auth,
path,
renderRoute,
showErrorMessage,
} = this.props;
const { uid } = auth;
return (
<Route
exact
key={key}
path={path}
render={
(!!uid && isLoaded(auth) && !isEmpty(auth)) ? renderRoute :
props => {
return (
<Redirect
to={{
pathname: '/login',
state: { from: props.location }
}} />
);
}
} />
);
}
}
这里的auth
道具来自withFirebase
HOC 注入。它附在this.props.firebase.auth
.
问题
.then
每当我成功登录时,都会在 promise 的函数中将用户数据返回给我firebase.login()
......当我到达 .push 导航的调度时,PrivateRoute 已经重新渲染而没有新的 auth 对象并且永远不会重新呈现。
我可以通过并观看它。它去:
- 登录成功
- 进入
.then
函数 - 通知用户登录成功
- 将导航更改为仪表板视图
- PrivateRoute 组件重新渲染并看到没有 UID 或 auth
firebase.login
承诺完全解决- 应用程序状态更新完成,firebase.auth 现在有 Uid 和所有其他配置文件/身份验证对象处于状态
- react-router BrowserRouter的容器,Switch和Route的运行正常
- 附加到此容器的视图重新渲染得很好,直到我返回 PrivateRoute 组件的函数
- PrivateRoute 渲染方法永远不会在第二次渲染中达到,
firebase.login
用户数据最终进入 firebase.auth 对象状态。所以现在我被授权了,但是由于没有渲染,它永远不会看到新的身份验证对象......
解决方案
推荐阅读
- react-native - 如果用户有权限切换到另一个屏幕时发出警告
- r - R闪亮代码通过单击生成excel选项卡
- active-directory - ADFS idpinitiatedsignon SAML 断言未签名
- mysql - TecDoc2016 数据库中的 SQL 请求返回不正确的结果
- android - firebase 远程配置 api 密钥是公开的还是秘密的?使用该 api 密钥可以做什么?
- python - 如何在不安装的情况下在 Mac 和 Windows 上运行 .py 3 文件
- amazon-rds - 我应该为符合 HIPAA 和 PCI-DSS 的数据使用单独的数据库吗?
- wso2 - 如何从修复传输 wso2 的响应中获取值
- sql - 第三高使用的编程语言 PostgreSQL
- angular - 如何以角度在一个垫表数据源中显示 2 个数组列表?