Javascript 无法从Redux存储读取状态。我错过了什么?
这是我的Javascript 无法从Redux存储读取状态。我错过了什么?,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,这是我的index.js,我在这里首先发送一个操作来读取我的位置列表: import 'babel-polyfill'; import React from 'react'; import { render } from 'react-dom'; import configureStore from './store/configureStore'; import {Provider} from 'react-redux'; import { Router, browserHistory }
index.js
,我在这里首先发送一个操作来读取我的位置列表:
import 'babel-polyfill';
import React from 'react';
import { render } from 'react-dom';
import configureStore from './store/configureStore';
import {Provider} from 'react-redux';
import { Router, browserHistory } from 'react-router';
import routes from './routes';
import {loadLocationList} from './actions/locationActions';
import './css/styles.css';
const store = configureStore();
render(
<Provider store={store}>
<Router history={browserHistory} routes={routes} />
</Provider>,
document.getElementById('app')
);
这是我的减速机:
import * as types from '../actions/actionTypes';
export default function locationReducer(state = [], action) {
switch(action.type) {
case types.LOAD_LOCATION_LIST_SUCCESS:
return {listingData: action.listingData};
default:
return state;
}
}
下面是我的MapStateTrops
&connect
功能:
function mapStateToProps(state, ownProps) {
return {
// we'll call this in our component -> this.props.listingData
listingData: state.listingData
};
}
export default connect(mapStateToProps, mapDispatchToProps)(homePage);
由于某种原因,它无法读取状态。列表数据,或者我真的做得不对吗?有人能帮我解决这个问题吗
我尝试记录state.listingData
,结果显示undefined
这是我的配置存储
:
import {createStore, applyMiddleware} from 'redux';
import rootReducer from '../reducers';
import reduxImmutableStateInvariant from 'redux-immutable-state-invariant';
import thunk from 'redux-thunk';
export default function configureStore(initialState) {
return createStore(
rootReducer,
initialState,
applyMiddleware(thunk, reduxImmutableStateInvariant())
);
}
这是我的组合减速器:
import {combineReducers} from 'redux';
import courses from './courseReducer';
import locations from './locationReducer';
const rootReducer = combineReducers({
courses,
locations
});
export default rootReducer;
我没有将它正确连接到商店吗
最新更新:
在MapStateTrops
中记录JSON.stringify(state)
实际上会显示结果。谢谢各位
正确的路径是state.locations.listingData
,因为我认为在我的组合减速器中,我将减速器包括为locations
,所以它的状态可能是state.locations
。希望这能帮助任何人解决这个问题。- 能否显示configureStore文件的代码?问题可能就在那里,可能是您忘了将减速机添加到减速机列表中
- 这个动作正确吗?您是否在
分派(loadLocationListSuccess(json))之前记录了数据代码>
import locations from './locationReducer';
const rootReducer = combineReducers({
courses,
locations
});
它使用这种结构创建存储:
const store = {
courses: {},
locations: {}
}
因此,在此之后,您调度了action并将数据更改为:
const store = {
courses: {},
locations: {
listingData: someData
}
}
如果要访问列表数据,如:state.listingData,则需要将reducer和combineReducer稍微更改为:
export default function listingData(state = {}, action) {
switch(action.type) {
case types.LOAD_LOCATION_LIST_SUCCESS:
return action.listingData;
default:
return state;
}
}
...
import listingData from './locationReducer';
const rootReducer = combineReducers({
courses,
listingData
});
我们无法知道您是否已将其正确连接到存储,因为尝试将其连接到存储的代码缺失:)您是否查看了react redux以及他们如何使用
connect
功能?此处似乎不够详细。(更多的是旁白,但默认状态应该是对象而不是数组)@ivarni我编辑了我的帖子,你可以看到我是如何使用connect
功能的@Gosha Arinich我只是指一个正在工作的项目,他们使用了对象。您可能认为我遗漏了什么?我已经为configureStore
编辑了我的帖子。我已经将我的reducer添加到我的组合rootReducer
。此外,我还设法在分派(loadLocationListSuccess(json))之前记录数据代码>。所以它应该是有效的。如果你需要的话,我可以给你看我的rootReducer
。是的,请给我看一下rootReducer。因为我认为,要访问listingData,您需要编写类似于state.locationReducer.listingData的东西,但它取决于rootReducer。另外,在MapStateTops中以JSON格式查看console.log的整个状态的结构,您可能会看到listingDataI已更新我的帖子的正确路径。我找到了它的正确路径,即state.locations.listingData
。只是一个问题,为什么它不是state.listingData
?我在我的帖子中提供的代码是相同的。为了将来的参考,请在评论中要求澄清,一旦你有了完整的答案,就发布答案。
export default function listingData(state = {}, action) {
switch(action.type) {
case types.LOAD_LOCATION_LIST_SUCCESS:
return action.listingData;
default:
return state;
}
}
...
import listingData from './locationReducer';
const rootReducer = combineReducers({
courses,
listingData
});