【问题标题】:Failed to store data in redux store无法在 redux 存储中存储数据
【发布时间】:2018-08-13 21:13:44
【问题描述】:

.创建了一个商店 .created 动作和reducers .connected store 到我的组件 .redux store 中存储数据失败

我必须使用 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));

【问题讨论】:

    标签: redux react-redux apollo-client


    【解决方案1】:

    您的 mapDispatchToProps 应如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 2017-12-04
      • 2013-06-01
      • 2023-03-20
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      相关资源
      最近更新 更多