两次单击以执行调度操作Redux

时间:2020-05-25 13:49:31

标签: javascript reactjs button redux

我在REACTJS应用程序中使用REDUX。我想在单击按钮后检索按钮ID,并将其发送到我的商店。仅在第二次单击后有效。你能帮助我吗 ?我的代码:

我的功能:

gotoidee (e)  {
let test= this.state.data[e.target.id];
console.log("test" +test.titre);
    
const action = { type: "SAVE_IDEE_ID", value:this.state.data[e.target.id]};
this.props.dispatch(action);
console.log(this.props.ideeId.titre);
}

const mapStateToProps = (state) => {
  return {

ideeId: state.saveIdee.ideeId
}
  }
  
export default connect(mapStateToProps)(liste_idee)

我的减速器:

const initialState = { ideeId: [] }

function saveIdee  (state = initialState, action) {
  let nextState
  switch (action.type) {
    case 'SAVE_IDEE_ID':
      nextState = {
        ...state,
        ideeId: action.value
    }
    return nextState

  default:
    return state
  }
}

export default saveIdee

我的按钮:

 <Button type="submit" id={ideeId} onClick={this.gotoidee}>Marche</Button>

1 个答案:

答案 0 :(得分:0)

gotoidee (e)  {
  // check here if the click is happening by putting a console here
  let test= this.state.data[e.target.id];
  console.log("test" +test);
  const action = { type: "SAVE_IDEE_ID", value:test};
  this.props.dispatch(action);
}

render(){
 console.log(this.props.ideeId); // check the updated value
}
相关问题