Sam*_*Sam 1 javascript reactjs redux
我在 Redux 中使用 React 并遵循教程,但我终生无法弄清楚这个错误。
未捕获的类型错误: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);
Run Code Online (Sandbox Code Playgroud)
动作/ 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
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏!
代替 :
export function fetchPosts () {....
Run Code Online (Sandbox Code Playgroud)
尝试:
export default function fetchPosts () {..
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4807 次 |
| 最近记录: |