我使用来自mern.io的入门模板。当我尝试运行以下代码时,出现错误:“ReferenceError: props is not defined”
EventDetailPage.js :
import React, {PropTypes} from 'react';
import {connect} from 'react-redux';
// Import Actions
import {fetchEvent} from '../../EventActions';
// Import Selectors
import {getEvent} from '../../EventReducer';
function EventDetailPage(props) {
return (
<div>
<h3>{props.event.name}</h3>
</div>
// null
);
}
// Actions required to provide data for this component to render in server side.
EventDetailPage.need = [params => {
return fetchEvent(params.id);
}
];
// Retrieve data from store as props
function mapStateToProps(state, props) {
return {
event: getEvent(state, props.params.id)
};
}
export default connect(mapStateToProps)(EventDetailPage);
fetchEvent(API 调用正常):
export function fetchEvent(id) {
return (dispatch) => {
return callApi(`events/${id}`).then(res => dispatch(addEvent(res.event)));
};
}
获取事件:
export const getEvent = (state, id) => state.events.data.filter(event => event.id === id)[0];
路线.js:
<Route
path="/events/:name-:id"
getComponent={(nextState, cb) => {
require.ensure([], require => {
cb(null, require('./modules/Event/pages/EventDetailPage/EventDetailPage').default);
});
}}
/>