【发布时间】:2017-02-03 09:07:05
【问题描述】:
我在试图弄清楚为什么我在状态中获取和存储的 JSON 数据没有被映射到我的组件 props 时遇到了困难,但在 console.log()ed 从 mapStateToProps() 函数中出现时却出现了。我做错了什么我在这里没有看到吗?
编辑: 显然 state 上的属性被映射到组件,但是是嵌套的。从mapStateToProps() 中登录时,data 属性必须通过state.state.data 访问。在此之前(请参阅 reducer),当记录 action.data 时,数据会按预期显示。没有奇怪的嵌套。
connect 函数可能有问题?
查看 state 对象中如何填充 state 属性:
下面是我的组件:
class ViewSelector extends Component {
componentWillMount() {
this.props.fetchDataAsync('VIEW ALL');
}
selectView(event) {
// this.props.data comes up undefined despite state being mapped
// to the component props
console.log('props: ' + this.props.data);
this.props.filterData(event.target.innerHTML, this.props.data);
}
render() {
return (
<section>
<nav>
<button onClick={this.selectView.bind(this)}>VIEW ALL</button>
<button onClick={this.selectView.bind(this)}>VIEW LAST WEEK</button>
<button onClick={this.selectView.bind(this)}>VIEW LAST MONTH</button>
</nav>
<Dashboard data={this.props.data}/>
</section>
);
}
}
function mapStateToProps(state) {
console.log(state);
// this console.log shows the state with the data property.
return {
data: state.data
};
}
export default connect(mapStateToProps, actions)(ViewSelector);
编辑:这是用于澄清的减速器:
import {
FETCH_DATA,
FETCH_DATA_SUCCESS,
FETCH_DATA_FAILURE,
FILTER_DATA
} from '../actions/types';
import { getLastWeek, getLastMonth } from './reducer_helper';
const initialState = {
error: '',
loading: false,
data: []
};
/* eslint-disable */
const app = (state = initialState, action) => {
switch(action.type) {
case FETCH_DATA:
return { ...state, error: '', loading: true };
case FETCH_DATA_SUCCESS:
// console.log(action.data) returns data as expected.
// no nesting...until it hits the mapStateToProps() function
return { ...state, error: '', loading: false, data: action.data };
case FETCH_DATA_FAILURE:
return { ...state, error: action.error, loading: false };
case FILTER_DATA:
let data;
if (action.view === 'VIEW LAST WEEK') {
data = getLastWeek(state.data);
} else if (action.view === 'VIEW LAST MONTH') {
data = getLastMonth(state.data);
} else {
data = state.data;
}
return { ...state, error: '', loading: false, data };
}
return state;
}
export default app;
Action Creators 澄清派发到 Reducer:
export function fetchData() {
return {
type: FETCH_DATA
};
}
export function fetchDataSuccess(data) {
return {
type: FETCH_DATA_SUCCESS,
data: data
};
}
export function fetchDataFailure(data) {
return {
type: FETCH_DATA_FAILURE,
data
};
}
export function filterData(view) {
return {
type: FILTER_DATA,
view
};
}
export function fetchDataAsync() {
return dispatch => {
dispatch(fetchData());
axios.get(DATA_URL)
.then(res => {
dispatch(fetchDataSuccess(res.data));
}).catch(err => {
dispatch(fetchDataFailure(err));
});
};
}
整个应用的渲染方式和 Redux 商店:
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import App from './components/app';
import reducers from './reducers';
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
ReactDOM.render(
<Provider store={createStoreWithMiddleware(reducers)}>
<App />
</Provider>
, document.querySelector('.container'));
【问题讨论】:
-
mapStateToProps 应该得到完整的状态作为参数,你到底在问什么?
-
当我返回
{ data: state.data }时,数据数组存在。但是当我尝试使用this.props.data访问组件内的数据时,它会出现未定义。所以它看起来就像在路上的某个地方迷路了。或者我完全误解了mapStateToProps在做什么...... -
从您的控制台日志中,看起来您在加载数据之前按下了按钮。还有一件事,尝试在地图函数中调用
state.state.data,看起来你的状态格式无效。向我们提供您的减速机。 -
您是如何创建商店的?它应该是:
import { createStore, applyMiddleware } from 'redux';import app from 'reducers/app'; //exported reducer from your exampleconst store = createStore(app, mymiddleware); -
你有多个reducer吗?你在打电话给 combineReducers 吗?如果你是...,你的状态会被调低
标签: javascript reactjs redux state