我正在處理反應,並且我有這3個文件(組件,動作和縮減器)。在這裏,他們是:道具是未定義的,但狀態已定義
cart.js看起來是這樣的:
import React, {Component} from 'react';
import { connect } from 'react-redux';
import { fetchMyWishlist } from '../actions/index';
class Cart extends Component {
constructor(props) {
super(props);
}
componentWillMount(){
this.props.fetchMyWishlist(this.props.signedInUserInfo._id);
}
}
renderWishList(){
console.log("ANOTHER TEST:"+ JSON.stringify(this.props.currentCart));
}
render(){
return ({this.renderWishlist()});
}
}
function mapStateToProps(state){
console.log("TEST: "+state.bookReducer.currentCart);
return {
currentCart: state.bookReducer.currentCart
};
}
export default connect(mapStateToProps, {fetchMyWishlist})(Cart);
我行動是這裏面的動作/ index.js:
export function fetchMyWishlist(userId){
let url = 'http://localhost:3001/cart/'+userId;
return function (dispatch) {
axios.get(url)
.then(response => {
dispatch({
type: FETCH_WISHLIST,
payload: response.data
});
});
}
}
這裏是我的減速:
import {FETCH_BOOKS} from '../actions/types';
import {FETCH_WISHLIST} from '../actions/types';
const INITIAL_STATE = { myBooks:[], currentCart:[] };
export default function(state = INITIAL_STATE, action) {
switch (action.type) {
case FETCH_BOOKS:
return { ...state, myBooks:action.payload };
case FETCH_WISHLIST:
return { ...state, currentCart: action.payload };
default:
return state;
}
}
我的問題是,我不明白爲什麼我從2只console.log
買這個2分的結果:
TEST: [object Object]
ANOTHER TEST: undefined
正如你所看到的減速做它的工作,但我不明白爲什麼this.props。 currentCart是未定義的。
不應'mapStateToProps'和'出口default'聲明的函數定義是_after_你'Cart'類聲明的右大括號? –