- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是完整的错误:
index.js:2177 Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.
这是什么意思?我在做什么会产生错误?
这是我的登录页面:
/* global gapi */
class SignIn extends React.Component{
constructor(props){
super(props);
this.state = {
redirect: false
}
this.onSignIn = this.onSignIn.bind(this)
this.updateRedirect = this.updateRedirect.bind(this)
// this.test = this.test.bind(this)
}
componentWillMount() {
console.log('componentWillMount ran')
gapi.load('auth2', () => {
gapi.auth2.init({
client_id: '817677528939-dss5sreclldv1inb26tb3tueac98d24r'
}).then((auth2) => {
console.log( "signed in: " + auth2.isSignedIn.get())
this.setState({
redirect: auth2.isSignedIn.get()
})
})
})
}
updateRedirect() {
this.setState({
redirect: true
})
}
componentDidMount() {
gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 300,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.onSignIn,
});
}
onSignIn(googleUser) {
console.log('this ran')
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
this.updateRedirect()
}
render() {
return (
this.state.redirect ?
<Redirect to='/options' />
:
<div>
<AppBar title="Pliny" showMenuIconButton={false} zDepth={2} />
<div id="my-signin2"></div>
</div>
)
}
}
export default SignIn
由于某种原因,当页面加载时,它正在加载 <div id="my-signin2"></div>
在重新路由之前的一瞬间。因此用户看到登录页面在屏幕上闪烁,然后被路由到正确的页面。
有没有办法让它在页面上的任何其他内容加载之前重新路由?
理想情况下,我想检查用户是否登录,如果登录,立即将他引导至特定页面。如果用户没有登录,则向他们显示登录页面(<div>
)
最佳答案
在您移动 gapi.load
之前,您将始终看到闪光灯方法进入更高的组件。它调用的函数是一个 promise ,这意味着您的组件将在请求该函数时继续呈现。我会将那部分移动到它自己的组件中更高一层,并让您的登录页面成为该组件的子组件。
编辑:
这是一个例子:
class AuthUser extends React.Component {
constructor(props){
super(props);
this.state = {
redirect: false
loaded: false
}
this.onSignIn = this.onSignIn.bind(this)
this.updateRedirect = this.updateRedirect.bind(this)
// this.test = this.test.bind(this)
}
onSignIn(googleUser) {
console.log('this ran')
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
this.updateRedirect()
}
componentWillMount() {
console.log('componentWillMount ran')
gapi.load('auth2', () => {
gapi.auth2.init({
client_id: '817677528939-dss5sreclldv1inb26tb3tueac98d24r'
}).then((auth2) => {
console.log( "signed in: " + auth2.isSignedIn.get())
this.setState({
redirect: auth2.isSignedIn.get(),
loaded: true
})
})
})
}
componentDidMount() {
gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 300,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.onSignIn,
});
}
updateRedirect() {
this.setState({
redirect: true
});
}
render() {
if(!this.state.loaded) {
return null
}
return this.state.redirect ? <Redirect to='/options' /> : <SignIn />;
}
}
class SignIn extends React.Component{
render() {
return (
<div>
<AppBar title="Pliny" showMenuIconButton={false} zDepth={2} />
<div id="my-signin2"></div>
</div>
)
}
}
现在,<SignIn />
在身份验证请求完成并且用户未被重定向之前,不会呈现组件。
关于javascript - 警告 : Can only update a mounted or mounting component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47359481/
我想在Rust FFI中使用'nix'库替换为'libc'。 我想使用nix::mount::mount()替换为libc::mount()。现在我有以下代码: libc::mount(ptr::nu
我正在使用React最新版本,当单击运行expandMenu()的按钮时,我收到以下错误 这里跟踪生命周期: constructor componentWillMount render compone
当我尝试通过命令行挂载目录“test_mount”时,操作成功: mount -t nfs4 remote_server_ip:/ local_dir 但我无法以编程方式挂载同一目录: int re
我有一个在外网运行的 kubernetes 集群,并在同一网络的另一台机器上设置了 NFS 服务器。我可以通过 ssh 连接到集群中的任何节点并通过运行 sudo mount -t nfs 10.17
这是我的组件: var booksRef = new Firebase("https://bookshelf.firebaseio.com/books"); class BookShelf exten
这是完整的错误: index.js:2177 Warning: Can only update a mounted or mounting component. This usually means
我想使用 C++ 挂载文件系统。我应该使用 mount() 系统调用,还是只执行 mount 二进制文件? 显然,系统调用会更快,我会花更少的时间构建命令行参数和解析错误消息等。但是,在阅读 moun
我正在尝试按照本教程进行操作:https://ivrodriguez.com/installing-self-signed-certificates-on-android/ 当我到达尝试将 /syst
我正在尝试按照本教程进行操作:https://ivrodriguez.com/installing-self-signed-certificates-on-android/ 当我到达尝试将 /syst
理论上,我应该能够异步获取一些数据并更新 componentDidMount 内的组件。这是我的组件: import React, { Component } from 'react' import
在使用 react-router 的辅助组件中时,我收到 setState 错误。任何人都可以看到我的代码中的任何问题吗? import React, { Component } from 'reac
我有 2 个组件,我通过使用 react router dom 中的 Route 组件在它们之间进行路由。其中一个组件在 componentDidMount 事件(使用 axios 包)上从虚拟 AP
我无法摆脱这个错误。当我从数组中删除一项并更新状态时,就会发生这种情况。 经过一些调试,我发现如果我重新加载应用程序,直接进入这个屏幕并删除,错误不会显示。但是,如果我导航到此屏幕,返回,然后再次转到
我尝试了 stackoverflow 和在线提供的所有命令和建议,但都没有用 https://stackoverflow.com/a/13366444/1441666 https://stackove
我有一个 RN 应用,在安装组件(“收藏夹”屏幕)时,我会从 Firebase 读取数据,并将其设置为收藏夹组件随后渲染的状态。 但是当我在另一个屏幕上并将项目添加到 firebase 中的收藏夹数据
我有一个使用 React Router 的应用程序。该应用程序有一些不断更新的组件(它是实时分析数据的仪表板)。我在使用 时遇到以下错误链接仪表板的各个部分: warning.js:36 Warni
我在 componentDidMount() 方法中使用 fetch(url, ...) 在 React Native 中获取数据。 所以我的类(class)看起来像 class Posts exte
相关问题here但我不确定如何使解决方案适应这个问题。 我正在尝试为带有选项卡的登录页面创建一个可重用的组件。每个选项卡都是可重用组件的子项,并且有自己的存储定义为 Prop :
我正在整理一个小 POC,其中一部分用于执行搜索功能。这个想法是“搜索”将负责以下事情: - 显示搜索输入表单(例如,文本、日期和位置参数) - 命中后端 AWS Lambda 搜索 API - 将结
我收到此错误setState(...):只能更新已安装或正在安装的组件。但我不知道如何修复它。 import React, { Component } from 'react'; import Loa
我是一名优秀的程序员,十分优秀!