我试图通过Redux的connect注入它们来定义NavigationButtons的单击事件侦听器。然而,这些函数没有被正确定义,即使它们通过了propTypes验证。下面的示例:
import React, { PropTypes } from "react"
import { connect } from "react-redux"
import { previousPage, nextPage } from "../actions"
const NavigationButtons = ( onPrevPageClick, onNextPageClick, firstPage = false ) => (
<div className="navigation-buttons">
{!firstPage && <div className="previous button" onClick={onPrevPageClick}>Previous Page</div>}
<div className="next button" onClick={onNextPageClick}>Next page</div>
<div className="clearfix"></div>
</div>
);
NavigationButtons.propTypes = {
onPrevPageClick: PropTypes.func.isRequired,
onNextPageClick: PropTypes.func.isRequired
};
const mapStateToProps = ({ page }) => {
return {
firstPage: page == 0
}
};
const mapDispatchToProps = (dispatch) => {
return {
onPrevPageClick: () => {
dispatch(previousPage())
},
onNextPageClick: () => {
dispatch(nextPage())
}
}
};
const NavigationButtonsContainer = connect(
mapStateToProps,
mapDispatchToProps
)(NavigationButtons);
export default NavigationButtonsContainer
函数声明中缺少括号。应该是:
const NavigationButtons = ({ onPrevPageClick, onNextPageClick, firstPage = false })
代替:
const NavigationButtons = (onPrevPageClick, onNextPageClick, firstPage = false)