這是完全錯誤:警告:只能更新一安裝或安裝組件
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>
)
Np,很高興你能工作! –
這不與承諾 – shorif2000
@ shorif2000這個例子是使用承諾 –