我想将存储的redux值传递给网页,然后显示它。但我总是得到这个错误
The previous state received by the reducer has unexpected type of "Function". Expected argument to be an object with the following keys: "session", "user", "form"
warning @ bundle.js:21260
combination @ bundle.js:21220
createStore @ bundle.js:20522
configurationStore @ bundle.js:34355
(anonymous) @ bundle.js:26863
__webpack_require__ @ bundle.js:20
(anonymous) @ bundle.js:26734
__webpack_require__ @ bundle.js:20
(anonymous) @ bundle.js:70
__webpack_require__ @ bundle.js:20
(anonymous) @ bundle.js:47
__webpack_require__ @ bundle.js:20
(anonymous) @ bundle.js:40
(anonymous) @ bundle.js:43
bundle.js:35708 Uncaught TypeError: Cannot read property 'type' of undefined
at sessionReducer (bundle.js:35708)
at combination (bundle.js:21230)
at createStore (bundle.js:20522)
at configurationStore (bundle.js:34355)
at Object.<anonymous> (bundle.js:26863)
at __webpack_require__ (bundle.js:20)
at Object.<anonymous> (bundle.js:26734)
at __webpack_require__ (bundle.js:20)
at Object.<anonymous> (bundle.js:70)
at __webpack_require__ (bundle.js:20)
所以这是我的redux商店:
function todos(state = [], action){
switch (action.type) {
case FETCH_CARS:
return state.concat([action.text]);
case GET_USER:
return state.concat([action.text]);
default:
return state;
}
}
const store = createStore(todos, 'Redux Store');
export default function configurationStore(initialState) {
return createStore(
todos,
rootReducer,
initialState,
applyMiddleware(thunk)
);
}
store.dispatch({
type: FETCH_CARS,
text: 'Redux store'})
store.dispatch({
type: GET_USER,
text: 'GET_USER'})
这是我的初始状态:
export default {
user: '',
email: '',
first_name: '',
last_name: '',
session: !!sessionStorage.jwt
}
以下是我在reducer中更新状态的方法
export default function sessionReducer(state = initialState.session, action) {
switch (action.type) {
case REGISTER_USER:
console.log(action.user);
return {
...state,
user: action.user
}
case FACEBOOKLOGIN:
console.log(action.user);
return {
...state,
user: action.user
}
case GET_USER:
console.log(action.user);
return {
...state,
user: action.user
last_name: action.user.last_name
}
case LOGIN_USER:
return state;
case LOGIN_SUCCESS:
browserHistory.push('/');
return !!sessionStorage.jwt;
case LOGOUT_USER:
browserHistory.push('/');
return !!sessionStorage.jwt;
default:
return state;
}
}
这是我需要显示值
的组件 getUser() {
return this.props.user.last_name;
}
render (){
return (
Hello {this.getUser()}
)
}
所以,我认为我的应用程序的初始状态有问题。但我不知道如何解决这个问题,谁能告诉我如何解决这个问题?
答案 0 :(得分:0)
问题源于您如何创建商店。你用更多的参数调用createStore
而不是它。方法签名为createStore(reducer, [preloadedState], [enhancer])
(https://github.com/reactjs/redux/blob/master/docs/api/createStore.md)。您需要使用combine
来合并rootReducer
和todos
(http://redux.js.org/docs/api/combineReducers.html)。