【发布时间】:2017-02-13 02:28:55
【问题描述】:
我正在使用 React 和 Redux 并按照教程进行操作,但我终生无法弄清楚这个错误。
未捕获的类型错误:this.props.fetchPosts 不是函数
我有一个包含 actions/index.js 的 src 文件和一个 components/posts_index.js 文件。据我所知,这是唯一涉及此错误的两个文件,我不相信 mapDispatchToProps 实际上是将我的函数放在 this.props 中,但我不知道为什么。
组件/posts_index.js
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);
actions/index.js
import axios from 'axios';
export const FETCH_POSTS = 'FETCH_POSTS';
const ROOT_URL = 'http://reduxblog.herokuapp.com/api';
const API_KEY = '?key=hn8y9e7yhvjkw9w887';
export function fetchPosts () {
const request = axios.get(`${ROOT_URL}/posts${API_KEY}`);
return {
type: FETCH_POSTS,
payload: request
}
}
非常感谢任何帮助!
【问题讨论】:
标签: javascript reactjs redux