我在Redux上使用React,并按照教程进行操作,但是我终生无法弄清楚这个错误。


  未捕获的TypeError: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);


动作/ 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
  }
}


任何帮助是极大的赞赏!

最佳答案

代替 :

export function fetchPosts () {....


尝试:

 export default function fetchPosts () {..

07-24 09:38
查看更多