2017-03-25 3 views
0

catch 핸들에서 this.setState를 사용하려고 할 때 이벤트 핸들러에서 mutate 함수를 호출하고 Invariant Violation 오류가 발생합니다.아폴로 돌연변이 콜백에서 Invariant Violation 설정 상태

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

바보 같은 실수 덕분에 –