Apollo mutate 回调中的 Invariant Violation 设置状态
Invariant Violation setting state in Apollo mutate callback
当我尝试在 catch 子句中使用 this.setState 时,我正在从事件处理程序调用 mutate 函数并收到不变冲突错误。
saveToken({data}){
let {userId, token, expires} = data.login;
storeLoginToken(userId, token, expires);
history.push('/dogs');
}
setErrors(error){
this.setState('error', error.graphQLErrors);
}
handleSubmit(event) {
event.preventDefault();
let {email, password} = this.state;
let {history} = this.props;
let clientKey = getClientKey();
this.props.mutate({ variables: { email, password, clientKey } })
.then(this.saveToken.bind(this))
.catch(this.setErrors.bind(this))
}
我认为问题在于您错过了 setState
函数中的左括号和右括号。
我会这样写:
class ClassName extends React.Component {
constructor(props) {
super(props);
this.state = {
error,
...
};
this.saveToken = this.saveToken.bind(this);
this.setErrors = this.setErrors.bind(this)
}
...
setErrors(error) {
this.setState({error: error.graphQLErrors}); // should work with brackets
}
handleSubmit(event) {
event.preventDefault();
let { email, password } = this.state;
let { history } = this.props;
let clientKey = getClientKey();
this.props.mutate({
variables: {
email,
password,
clientKey
}
})
.then(this.saveToken)
.catch(this.setErrors)
}
}
当我尝试在 catch 子句中使用 this.setState 时,我正在从事件处理程序调用 mutate 函数并收到不变冲突错误。
saveToken({data}){
let {userId, token, expires} = data.login;
storeLoginToken(userId, token, expires);
history.push('/dogs');
}
setErrors(error){
this.setState('error', error.graphQLErrors);
}
handleSubmit(event) {
event.preventDefault();
let {email, password} = this.state;
let {history} = this.props;
let clientKey = getClientKey();
this.props.mutate({ variables: { email, password, clientKey } })
.then(this.saveToken.bind(this))
.catch(this.setErrors.bind(this))
}
我认为问题在于您错过了 setState
函数中的左括号和右括号。
我会这样写:
class ClassName extends React.Component {
constructor(props) {
super(props);
this.state = {
error,
...
};
this.saveToken = this.saveToken.bind(this);
this.setErrors = this.setErrors.bind(this)
}
...
setErrors(error) {
this.setState({error: error.graphQLErrors}); // should work with brackets
}
handleSubmit(event) {
event.preventDefault();
let { email, password } = this.state;
let { history } = this.props;
let clientKey = getClientKey();
this.props.mutate({
variables: {
email,
password,
clientKey
}
})
.then(this.saveToken)
.catch(this.setErrors)
}
}