Javascript mapDispatchToProps没有将我的功能放入道具中
我正在使用React with Redux并遵循一个教程,但我一辈子都无法找出这个错误 未捕获类型错误:this.props.fetchPosts不是函数 我有一个带有actions/index.js的src文件,还有一个components/posts\u index.js文件。据我所知,这是这个错误中仅有的两个文件,我不相信mapDispatchToProps会将我的函数放在这个.props中,但我不知道为什么 组件/帖子_index.jsJavascript mapDispatchToProps没有将我的功能放入道具中,javascript,reactjs,redux,Javascript,Reactjs,Redux,我正在使用React with Redux并遵循一个教程,但我一辈子都无法找出这个错误 未捕获类型错误:this.props.fetchPosts不是函数 我有一个带有actions/index.js的src文件,还有一个components/posts\u index.js文件。据我所知,这是这个错误中仅有的两个文件,我不相信mapDispatchToProps会将我的函数放在这个.props中,但我不知道为什么 组件/帖子_index.js import React, { Component
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import fetchPosts from '../actions/index';
class PostsIndex extends Component {
componentWillMount() {
this.props.fetchPosts();
}
render() {
return(
<div>List of Blog Posts</div>
)
}
}
function mapDispatchToProps (dispatch) {
return bindActionCreators({ fetchPosts }, dispatch);
}
export default connect(null, mapDispatchToProps)(PostsIndex);
非常感谢您的帮助 而不是:
export function fetchPosts () {....
尝试:
而不是:
export function fetchPosts () {....
尝试:
在actions/index.js中,更改为导出默认值
导出默认函数fetchPosts(){
const request=axios.get(`${ROOT\u URL}/posts${API\u KEY}`);
返回{
类型:获取_POSTS,
有效载荷:请求
}
}
在actions/index.js中,更改为导出默认值
导出默认函数fetchPosts(){
const request=axios.get(`${ROOT\u URL}/posts${API\u KEY}`);
返回{
类型:获取_POSTS,
有效载荷:请求
}
}
在index.js中,从作用域更改导入
这样做
从“/Components/posts\u index”导入PostsIndex代码>
而不是从“/Components/posts_index”导入{PostsIndex}”代码>在index.js中更改从作用域的导入
这样做
从“/Components/posts\u index”导入PostsIndex代码>
而不是从“/Components/posts_index”导入{PostsIndex}”代码>就是这样!非常感谢。我有点困惑,因为我调用了import{fetchPosts}from…,它不应该找到fetchPosts吗?当你为函数使用默认值时,它知道要导入什么,所以不需要括号。就是这样!非常感谢。我有点困惑,因为我从…调用了import{fetchPosts},它不应该找到fetchPosts吗?当你为函数使用default时,它知道要导入什么,所以不需要括号。
export default function fetchPosts () {..