2017-03-25 46 views
0

我在事件處理函數中調用mutate函數,並在嘗試在catch子句中使用this.setState時收到不變違例錯誤。Apollo 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)) 
} 

回答

0

我認爲問題在於你錯過了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) 
 
    } 
 

 
}

+0

愚蠢的錯誤,謝謝 –