我正在使用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 –