Javascript this.props.getEvents不是函数
我在我的Javascript this.props.getEvents不是函数,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,我在我的eventActions.js中导出了中定义的getEvents。我不明白为什么会出现这个错误。我导入了函数,文件路径是正确的 组件 import { getEvents } from "../../actions/eventActions"; ... componentDidMount() { this.props.getCurrentProfile(); this.props.getEvents(); } eventActions.js // Get Events ex
eventActions.js
中导出了中定义的getEvents。我不明白为什么会出现这个错误。我导入了函数,文件路径是正确的
组件
import { getEvents } from "../../actions/eventActions";
...
componentDidMount() {
this.props.getCurrentProfile();
this.props.getEvents();
}
eventActions.js
// Get Events
export const getEvents = () => dispatch => {
dispatch(setEventsLoading());
axios
.get("/api/events")
.then(res =>
dispatch({
type: GET_EVENTS,
payload: res.data
})
)
.catch(err =>
dispatch({
type: GET_EVENTS,
payload: null
})
);
};
Props
是父组件传递给其子组件的东西。这里,您是从文件导入它,而不是从父组件传递
您可以简单地使用-
getEvents()
而不是this.props.getEvents()
我没有用connect注入它。上述建议同样有效。我想我需要这个组件之外的东西,所以最好的选择是用connect注入它
export default connect(
mapStateToProps,
{ getCurrentProfile, deleteAccount, getEvents }
)(Dashboard);
您正在将它导入到模块中,但您是否真的将
getEvents
作为组件的道具?试着只写getEvents()代码>取而代之。这很有效。为什么我可以调用this.props.getCurrentProfile()
,但不能调用事件?谢谢你一直回答我的问题!不客气!如果看不到使用组件的代码,这很难说。使用组件时,您可能会将getCurrentProfile
作为道具传入组件,或者您可能会使用Reduxconnect
等注入组件。我没有使用connect注入它!事情就是这样!感谢您一如既往地放弃知识!为什么我可以使用this.props.getCurrentProfile()
我试着像getCurrentProfile()
那样调用它,但它没有加载?你和Thole的建议奏效了。因为,这可能是你在问题中提到的组件的父组件传递的。你能把代码贴在创建这个组件的地方吗?