我试图使ajax请求与按下按钮上的react-redux,但当onClick操作尝试从操作组件启动功能时出现错误。react-redux,onClick不会启动动作功能(undefined)
post_data.js
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import getPostsData from '../actions/index';
class PostButton extends Component{
constructor(props){
super(props);
this.GetPostData = this.GetPostData.bind(this);
}
GetPostData(event){
event.preventDefault();
this.props.getPostsData();
}
render(){
return(
<div style={{marginTop: 50 + 'px'}}>
<button className="btn btn-success" onClick={this.GetPostData}>Click Here to Load Posts</button>
</div>
)
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators({getPostsData}, dispatch);
}
export default connect(null, mapDispatchToProps)(PostButton);
操作:
import axios from 'axios';
export const GET_POSTS_DATA = 'GET_POSTS_DATA';
export function getPostsData() {
const url = `https://jsonplaceholder.typicode.com/posts`;
const request = axios.get(url);
console.log(request);
return{
type: GET_POSTS_DATA,
payload: request
}
}
减速机:
import GET_POSTS_DATA from '../actions/index';
export default function(state = [], action){
switch (action.type){
case GET_POSTS_DATA:
return [action.payload, ...state]
}
return state;
}
控制台错误: 遗漏的类型错误:this.props.getPostsData不是一个函数 这涉及到:
GetPostData(event){
event.preventDefault();
this.props.getPostsData();
}
任何帮助表示赞赏...
谢谢你,尼克!:)我知道这是愚蠢的,它是:))))作为魔术后,我输入正确。 – SHCodeR