gpt4 book ai didi

javascript - 警告 : Can only update a mounted or mounting component

转载 作者:行者123 更新时间:2023-11-30 20:58:39 26 4
gpt4 key购买 nike

这是完整的错误:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com