2017-10-10 43 views
0

我在组件时遇到错误,然后点击里面的Link并尝试找回。在链接后渲染组件时出错 - 反应

基本上流程是: WeddingList- >点击- >Wedding Page- >点击- >WeddingList

所以,当我点击回去,就会触发这个错误:

TypeError: this.props.weddings.map is not a function WeddingList.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 
 
});

在此先感谢

回答

0

它看起来像this.props.weddingsundefined第一渲染。如果通过ajax请求收到婚礼,可能会发生(fetchWeddings这样做)。解决的办法是设置在你reducers.js初始状态:

import { FETCH_WEDDINGS, FETCH_WEDDING } from '../actions/types'; 

const initialState = { weddings: [] } 

export default function(state = initialState, action) { 
    switch (action.type) { 
    case FETCH_WEDDINGS: 
     return {...state, weddings: action.payload.weddings}; 
    case FETCH_WEDDING: 
     return {...state, wedding: action.payload.wedding}; 
    default: 
     return state; 
    } 
} 

此外,您还可以验证PropTypesWeddingList.js

import PropTypes from 'prop-types'; 

class WeddingList extends Component { 
    static propTypes = { 
    wedding: PropTypes.array.isRequired 
    } 
    .... 
} 
+0

与PropTypes该解决方案没有奏效。 如何处理initialState?我会在你的问题上包括我的减速器js。 –

+0

PropTypes会验证您的道具并在道具不正常时记录错误。所以你应该通过有效的婚礼 –

+0

我得到的componentWillMount()婚礼列表。它在我进入页面或刷新页面时起作用。但是当我离开页面并再次输入时,它不起作用。 –