我正在使用React Redux构建应用程序。状态未定义(reactJS)
这是我的减速器:
import { INCOME_LIST } from '../actionTypes'
import Immutable from 'immutable'
const initialUserState = {
list: [{
id: 1,
label: 'List item 1'
},
{
id: 2,
label: 'List item 2'
}]
}
const listReducer = function(state = [initialUserState], action) {
switch(action.type) {
case 'INCOME_LIST':
return Object.assign({}, state, { list: action.data });
default: return state;
}
}
export default listReducer
这是我的组件:
import React, { Component, PropTypes } from 'react'
import { Link, browserHistory } from 'react-router'
import { connect } from 'react-redux'
import axios from 'axios'
class Income extends Component {
constructor(props) {
super(props)
}
}
render() {
console.log(this.props.items)
return (
<div>
test
</div>
)
}
}
const mapStateToProps = function(store) {
return {
items: state.list
};
}
export default connect(mapStateToProps)(Income);
控制台说: '未定义' 为什么执行console.log(this.props.items)获得undifined? 如何正确的方式从减速机获得状态?可能在那里犯错?
这样做,但在控制台中我得到未定义.....为什么我无法获得状态形式我的reducer? –
尝试和console.log(state.list)里面在mapStateToProps功能,看看它是否会记录什么 –
进行屏幕https://i.imgsafe.org/4e36750cf3.png –