0
我在組件時遇到錯誤,然後點擊裏面的Link
並嘗試找回。在鏈接後渲染組件時出錯 - 反應
基本上流程是: WeddingList
頁- >點擊- >Wedding Page
- >點擊- >WeddingList
所以,當我點擊回去,就會觸發這個錯誤:
TypeError:
this.props.weddings.map
is not a functionWeddingList.renderWeddings
這裏是我的代碼:
個WeddingList.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { fetchWeddings } from '../../actions';
import { Link } from 'react-router-dom';
class WeddingList extends Component {
componentWillMount() {
this.props.fetchWeddings();
}
renderWeddings() {
return this.props.weddings.map(wedding => {
return (
<div className="row" key = { wedding._id }>
<div className="col s10 offset-s1" key={wedding._id}>
<div className="card blue-grey darken-1" key={wedding._id}>
<div className="card-content white-text">
<span className="card-title">{wedding.coupleName1} & {wedding.coupleName2}</span>
</div>
<div className="card-action">
<Link
to={'/wedding/'+wedding.pageName}
className="btn">
Visit!
</Link>
</div>
</div>
</div>
</div>
);
})
}
render() {
return (
<div>
{this.renderWeddings()}
</div>
);
}
}
function mapStateToProps({ weddings }) {
return { weddings };
}
export default connect(mapStateToProps, { fetchWeddings })(WeddingList);
Wedding.js
import React from 'react';
import WeddingPage from './weddings/WeddingPage';
import { Link } from 'react-router-dom';
const Wedding = props => {
return (
<div>
<WeddingPage {...props}/>
<Link to={'/weddings'} className="btn">Back</Link>
</div>
);
}
export default Wedding;
weddingsReducer.js
個import { FETCH_WEDDINGS, FETCH_WEDDING } from '../actions/types';
export default function(state = [], action) {
switch (action.type) {
case FETCH_WEDDINGS:
return action.payload;
case FETCH_WEDDING:
return action.payload;
default:
return state;
}
}
reducers.js
import { combineReducers } from 'redux';
import { reducer as reduxForm } from 'redux-form'
import authReducer from './authReducer';
import weddingsReducer from './weddingsReducer';
export default combineReducers({
auth: authReducer,
form: reduxForm,
weddings: weddingsReducer,
wedding: weddingsReducer
});
在此先感謝
與PropTypes該解決方案沒有奏效。 如何處理initialState?我會在你的問題上包括我的減速器js。 –
PropTypes會驗證您的道具並在道具不正常時記錄錯誤。所以你應該通過有效的婚禮 –
我得到的componentWillMount()婚禮列表。它在我進入頁面或刷新頁面時起作用。但是當我離開頁面並再次輸入時,它不起作用。 –