redux / axios에 문제가 있습니다. axios 요청이 성공한 후 스토어를 변경하고 싶습니다. 스토어 변경 후 애플리케이션 상태를 변경하고 싶습니다. 코드가 내 기능에 도달 할 것으로 예상했지만 그런 일이 발생하지 않으며 그 이유가 확실하지 않습니다! redux는 비동기입니까? 그렇다면 어떻게 동기화 전화를 걸 수 있습니까? (일부 코드 아래에서 왼쪽)
axios.post(url,{
"system":system,
})
.then(res => {
this.props.setTest(res) #code does not follow this line, no state is set
this.setState({
test_data:res,
redirect:true
})
})
.catch((error)=>{
if(error.response){
if(error.response.data["Error"]){
console.log("There was an error fetching the test")
}
}
})
export function setTest(test){
return {type: "SET_TEST",payload: test}
}
case "SET_TEST":{
console.log("SET_TEST")
return {
...state,
(and other changed args)
};
}
도와 주셔서 감사합니다!
this.props.setTest(res)
일종의 오류를 유발할 가능성이 매우 높습니다 .
나중에 catch
매우 특정한 오류 만 처리 하는 블록 이 있기 때문에이 오류는 절대로 발생하지 않으며 console.log하기 전까지는 처음에 오류가 무엇인지 알 수 없습니다. 그래서:
.catch((error)=>{
if(error.response && error.response.data["Error"]){
console.log("There was an error fetching the test")
} else {
// don't swallow your error, log it!
console.log("some unexpected error happened:", error);
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다