2017-10-04 1 views
0

나는 맹독성을 달성하기가 어렵다. 나는 몇 가지 질문을 읽었으며 또한 안내하지만 올바르게 이해할 수는 없습니다.ReactJS - Thunk - 2 개의 이벤트가 차례로 동기화되지 않음.

나는 연결해야한다는 것을 알고 있지만 나는 모두 잃어버린 다. 도와주세요.

단일 발송은 잘되지만 이중 발송은 전혀 작동하지 않습니다. 로그가 인쇄되지 않습니다.

handleSubmit(event) 
{ 
    event.preventDefault(); 
    this.isLoading = true; 

    // TWO EVENTS needed, first to add new player and then set as bowler 
    if (this.state.showAddBowlerTextfield) { 

     utilPostJSON(jsonURL + 'add-player.php', { 
      name: this.state.bowlerName, 
      matchId: this.currState.matchId, 
      inningsId: this.currState.inningsId, 
      teamId: this.currState.teamBowling.id, 
      isBowler: true 
     }) 
      .then((response) => { 
       console.log('Success', response); 

       this.setState({ 
        bowlerId: response.data.id, 
        bowlerName: response.data.name, 
       }); 

       this.dispatchAddPlayerAndAddBowler(); 
      }) 
      .catch((error) => { 
       console.error('Error', error); 
      }); 

     return 
    } 


    // This needs only one dispatch 
    const {store} = this.context; 
    this.currState = store.getState(); 

    store.dispatch({ 
     type: constants.NEW_BOWLER, 
     payload: this.state 
    }); 

    this.props.parentClose(); 
} 

//This function sends two dispatches 
dispatchAddPlayerAndAddBowler() 
{ 
    console.log('dispatchAddPlayerAndAddBowler'); 

    return (dispatch, getState) => { 
     const {store} = this.context; 
     console.log('Store', store); 

     store.dispatch({ 
      type: constants.NEW_PLAYER_BOWLER, 
      payload: this.state 
     }); 
     store.dispatch({ 
      type: constants.NEW_BOWLER, 
      payload: this.state 
     }); 
    } 
} 

로그의 출력은 다음과 같습니다 당신이 잘못 썽크를 사용하는

Success {data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, …} 
dispatchAddPlayerAndAddBowler 

답변

0

. mapDispatchToProps` 당신이 말해 주시겠습니까`

dispatchAddPlayerAndAddBowler() { 
    console.log('dispatchAddPlayerAndAddBowler'); 

    return (dispatch, getState) => { 
    // Wrong: const {store} = this.context; 
    // you don't need to do this dispatch is already there in the first parameter 

    dispatch({ 
     type: constants.NEW_PLAYER_BOWLER, 
     payload: this.state 
    }); 

    dispatch({ 
     type: constants.NEW_BOWLER, 
     payload: this.state 
    }); 
    } 
} 

는 또한 당신은 내가`connect`을 이해하지 못했다 handleSubmit

+0

에 대한 connectmapDispatchToProps를 사용해야하고 : dispatchAddPlayerAndAddBowler 리팩토링보십시오. '기능 mapDispatchToProps (파견) {' 창 { onHandleSubmit (this.state) => { 디스패치 ({ 유형 : constants.NEW_PLAYER_BOWLER, 페이로드 : this.state }) }} } ' – Sallu

+0

[docs] (http://redux.js.org/docs/basics/UsageWithReact.html)를 보셨습니까? 일단'connect'를 사용하면'this.props.onHandleSubmit'을 사용하여 소품에서 onHandleSubmit을 얻어야하고'this.props.onHandleSubmit (this.state)를 호출하여'handleSubmit' 인스턴스 메소드의 마지막 비트에서 호출해야합니다)'. 또한 핸들러에서 단순히'this.state'를 사용하면 안됩니다.'onHandleSubmit : state => {dispatch ({type : constants.NEW_PLAYER_BOWLER, payload : state})}' – excalliburbd

+0

늦게 돌아가서 죄송합니다. 나는 문서를 읽고 시험해야했다. 소중한 시간을 가져 주셔서 감사합니다. – Sallu

관련 문제