无法在redux store

时间:2018-03-06 04:53:01

标签: redux react-redux apollo-client

。创建一个商店 。创造的行动和减少者 .connected store到我的组件 。无法在redux商店中存储数据

我必须使用redux将添加事件存储在日历中,这些值不会存储在redux中。

//动作

export const saveUserEvents=(events)=>{
   return {
     type:'SAVE_USER_EVENTS',
     payload: events
   }
}

//减速

export  const form=(state = initialState, action) => {
switch (action.type) {
  case "SAVE_USER_EVENTS":
return {...state, events: action.payload};
default:
return state
}
}

//商店

 let reducers=combineReducers({
 form
 })
export default createStore(
reducers,
composeWithDevTools(
applyMiddleware(thunk)
)
)

//组件

Form(){
var title = document.getElementById("title").value
var description  = document.getElementById("description").value
var start  = document.getElementById("start").value
var end  = document.getElementById("end").value

                  if(title!=""){
                    alert("Event Added Successfully")
                  this.props
                  .form({
                    variables: {
                      title:title,
                      description:description,
                      start:start,
                      end: end,
                      user_id: localStorage.getItem("id")

                    }
                  })
                  .then(({ data }) => {
                    alert("data is "+JSON.stringify(data.form))
                    if(data.form.message == "Event saved"){
                      this.props.saveUserEvents(data.form);
                      //alert("success")
                    //this.props.history.push('/Calender')
                      window.location.href="/Calendar";
                      return true;
                    }else {
                      alert("failure")
                      document.getElementById("messagegeneralNumber").innerHTML = '';
                    }
                  })
                  .catch(error => {
                    alert("data is "+JSON.stringify(error))
                  })
                }

                  else{
                    alert("data not loading")
                  }

                }

//连接到store-redux。使用mapstatetoprops和mapdispatchtoprops

const mapStateToProps=(state, ownProps)=>{
return {
form: state
}
}
const mapDispatchToProps={
saveUserEvents
}
const Event = compose(
graphql(mutations.FORM,{name:'form'})
)(AddEvent);
export default withRouter(connect(mapStateToProps 
,mapDispatchToProps)
(Event));

1 个答案:

答案 0 :(得分:1)

您的mapDispatchToProps应如下所示:

const mapDispatchToProps = (dispatch) => {
   saveUserEvents: (events)=>{
    dispatch(saveUserEvents(events)); <--- here is your action creator that have to be dispatched
   }

}