gpt4 book ai didi

javascript - 无法从 Redux 存储读取状态。我错过了什么?

转载 作者:行者123 更新时间:2023-11-30 15:57:07 24 4
gpt4 key购买 nike

这是我的 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')
);

然后这是我获取数据然后从中创建一个操作的操作:

export function loadLocationListSuccess(alistingData) {
return { type: types.LOAD_LOCATION_LIST_SUCCESS, listingData: alistingData};
}

export function loadLocationList() {
return function(dispatch){ //we return a function that accepts a parameter, we just called it dispatch
//dispatch(fetchCallActions.fetchCallStart("")); // we dispatch a function fetchCallStart to indicate the start of our call, this is to keep in check with our asynchronous function calls
let link = 'http://example.com:8399/location';//our fetch url
console.log(link); //we log our link, just for debug purposes

return fetch(link) //start fetch
.then(function(response) {
return response.json();
}).then(function(json) {
dispatch(loadLocationListSuccess(json));
}).catch(function(ex) {
console.log('parsing failed', ex);

});
};
}

然后这是我的 reducer :

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;
}
}

然后这是我的 mapStateToProps & 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 还是我实际上做错了?谁能帮我解决这个问题?

我尝试记录 state.listingData 并显示 undefined

这是我的configureStore:

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())
);
}

这是我的组合 Reducer:

import {combineReducers} from 'redux';
import courses from './courseReducer';
import locations from './locationReducer';

const rootReducer = combineReducers({
courses,
locations
});


export default rootReducer;

我没有正确连接到商店吗?

最近更新:在 mapStateToProps 中记录 JSON.stringify(state) 实际上会显示结果。谢谢大家。

正确的路径原来是 state.locations.listingData 因为我认为在我的组合 Reducer 中我将 reducer 包含在 locations 中所以也许这就是它的状态的原因是 state.locations。希望这可以帮助任何人解决这个问题。

最佳答案

  • 你能展示configureStore文件的代码吗?问题可能就在那里,可能是您忘记将 reducer 添加到 reducer 列表中。
  • 行动是否有效?您是否在 dispatch(loadLocationListSuccess(json)); 之前记录了数据?

更新:

因为 rootReducer。每个 reducer 在 store 中创建自己的 key 。当您在 rootReducer 中组合您的 reducer 时,例如:

import locations from './locationReducer';
const rootReducer = combineReducers({
courses,
locations
});

它创建具有这种结构的商店:

const store = {
courses: {},
locations: {}
}

因此,在您发送 action 和 reducer 之后将数据更改为:

const store = {
courses: {},
locations: {
listingData: someData
}
}

如果你想访问像 state.listingData 这样的 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
});

关于javascript - 无法从 Redux 存储读取状态。我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38343128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com