我碰到了一堵牆,試圖找出爲什麼我要獲取和存儲狀態的JSON數據沒有映射到我的組件道具,但在mapStateToProps()
函數中出現console.log()
時出現。我做錯了什麼,我沒有看到這裏?mapStateToProps()在Redux應用程序嵌套狀態的狀態?
編輯:顯然狀態屬性被映射到組件,但嵌套。 data
財產,從mapStateToProps()
登錄時,必須通過state.state.data
訪問。在此之前(請參閱減速器),當記錄action.data
時,數據按預期顯示。沒有奇怪的嵌套。 connect
功能可能有問題嗎?
瞭解如何有狀態對象中填充的狀態屬性:
下面是我的組件:
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;
動作創作者的澄清對派遣到減速機:
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));
});
};
}
如何將整個應用正在呈現&的終極版店:
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應該獲得完整的狀態作爲參數,你到底在問什麼? – StateLess
當我返回'{data:state.data}'數據數組存在時。但是當我嘗試使用'this.props.data'訪問組件內的數據時,它會出現未定義的情況。所以它*看起來像是沿着道路某處正在迷路。或者我完全誤解了mapStateToProps正在做什麼...... – Jose
你可以嘗試登錄渲染並檢查一次 – StateLess