在我的reducer中更新状态时遇到问题。
我有以下减速器:
const initialState = {
isLoading: false,
isAuthorised: false,
username: null,
jwt: null,
SuburbPostcodeDropDownList: null,
StateDropDownList: null,
CompanyStateShortName: null,
error: null,
}
export default (state = initialState, action) => {
switch (action.type) {
case REQUEST_LOGIN_TOKEN:
return {
...state,
isLoading: true,
isAuthorised: false,
username: action.payload,
jwt: null,
error: null,
}
case RECEIVE_LOGIN_TOKEN:
return {
...state,
isLoading: false,
isAuthorised: true,
jwt: action.payload,
error: null,
}
case ERROR_LOGIN_TOKEN:
return {
...state,
isLoading: false,
isAuthorised: false,
username: null,
jwt: null,
error: action.payload,
}
case REQUEST_SELECT_DATA:
return {
...state,
isLoading: true,
isAuthorised: false,
jwt: action.payload,
SuburbPostcodeDropDownList: null,
StateDropDownList: null,
CompanyStateShortName: null,
error: null,
}
case RECEIVE_SELECT_DATA:
return {
...state,
isLoading: false,
isAuthorised: true,
SuburbPostcodeDropDownList: action.payload.SuburbPostcodeDropDownList,
StateDropDownList: action.payload.StateDropDownList,
CompanyStateShortName: action.payload.CompanyStateShortName
}
它与有效负载一起到达RECEIVE_LOGIN_TOKEN。其中两个是数组。这是一张图片,其中包含了正确的案例选项及其数据。
当它完成并且我在chrome中查看redux扩展时它没有更新状态。
我无法解决为什么它没有更新状态?我已将返回数据的名称设置为州名等,但它不会从初始状态更改,甚至注册它们都存在于树的登录部分。
想知道我做错了什么?
修改
这里要求的是执行调度的代码:
export const requestLoginToken = (username, password) =>
(dispatch, getState) => {
dispatch({ type: REQUEST_LOGIN_TOKEN, payload: username })
const payload = {
userName: username,
password: password,
}
const task = fetch('/api/jwt', {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
})
.then(handleErrors)
.then(response => response.json())
.then(data => {
dispatch({ type: RECEIVE_LOGIN_TOKEN, payload: data })
saveJwt(data)
//selectData()
dispatch({ type: REQUEST_SELECT_DATA })
const token = getJwt()
const headers = new Headers({
'Authorization': `Bearer ${token}`
})
const retrieveSelectData = fetch('/api/SelectData/SelectData', {
method: 'GET',
headers,
})
.then(handleErrors)
.then(response => response.json())
.then(selectData => {
dispatch({ type: RECEIVE_SELECT_DATA, payload: selectData })
})
})
.catch(error => {
clearJwt()
dispatch({ type: ERROR_LOGIN_TOKEN, payload: error.message })
})
addTask(task)
return task
}
它通过retrieveSelectData ...
获取数据答案 0 :(得分:2)
当访问reducer中的有效负载时,你应该将第一个字母大写为小写,所以
SuburbPostcodeDropDownList: action.payload.suburbPostcodeDropDownList
而不是:
SuburbPostcodeDropDownList: action.payload.SuburbPostcodeDropDownList