如何使用Redux Thunk将元素添加到数组中?

时间:2017-06-18 19:19:14

标签: reactjs redux react-redux redux-thunk

我正在创建一个应该允许教师向其仪表板添加课程的应用程序,只是课程名称没有保存到商店。



export const addCourse = (name, instructor) => {
  return function(dispatch) {
    axios.post(`http://localhost:3001/api/courses`, {
      name: name,
      _creator: instructor
    })
    .then(response => {
      cookies.get('instructor', response.data.instructor)
      dispatch({
      type: ADD_COURSE,
      coursename: name,
      _creator: cookies.get('instructor')
      })
      console.log(response.data)
    })
  }
}




和减速器低于



const INITIAL_STATE = {
 coursename: [''],
 _creator: [{}]
}

export default function(state = INITIAL_STATE, action) {

  switch(action.type) {
    case ADD_COURSE:
      return {...state,
        coursename: [...state.coursename, action.coursename],
        _creator: action._creator
      }
  }
  return state;
}




我错过了用户的输入无法保存到商店的内容?

0 个答案:

没有答案