[英]Dispatch is not a function in reducer
MY action我的行动
const fetchDataApi = (getState) => {
let { data } = getState()
return axios.get('http://api.openweathermap.org/data/2.5/weather?q=london,uk&appid=26aacf43db7ecfa2ecd85500eaee9920').then(thunkdata => {
console.log(thunkdata)
return {
[data]: thunkdata
}
})
}
const fetchgetDataCall = () => {
return (dispatch, getState) => {
return dispatch(fetchDataApi(getState))
}
}
export const getData = (dispatch) => {
dispatch(fetchgetDataCall())
return {
type: actionTypes.GETDATA,
}
}
In action.js i want to get data from my whether api and store in data, so i am using getstate to get data variable and assign data to it在 action.js 中,我想从我的 api 获取数据并存储在数据中,所以我使用 getstate 来获取数据变量并将数据分配给它
My calender Component where i am connecting my callender to actionType我的日历组件,我将调用者连接到 actionType
import React, { Component } from 'react';
// import 'moment/locale/it.js';
import { DatePicker, DatePickerInput } from 'rc-datepicker';
// import { ca } from 'date-fns/esm/locale';
import 'rc-datepicker/lib/style.css';
import { connect } from 'react-redux';
import { getData } from '../store/actions/actions'
const date = '2015-06-26' // or Date or Moment.js
class Callender extends Component {
//These is a method es7
onChangeandler = (jsDate, dateString, event) => {
// event.preventDefault()
console.log("[we are lokking at js date]",jsDate);
this.props.getWether();
console.log("[we are seeing the props storeDta]",this.props.storeData);
}
//Next method
render() {
return (
<div>
<DatePicker onChange={this.onChangeandler} value={date} />
</div>
)
}
}
const mapStateToProps = state =>({
storeData: state.data
})
const mapDispatchToProps = (dispatch) =>({
getWether: () => dispatch(getData())
})
export default connect(mapStateToProps,mapDispatchToProps)(Callender)
My reducer我的减速机
import * as actionType from '../actions/actionTypes';
const intialState ={
time:null,
day:null,
data:null
}
// reducer
const reducer = (state=intialState, action) =>{
switch(action.type){
case actionType.GETDATA:
return {
...state,
data:action.data
}
case actionType.POSTDATA:
return {
...state
}
default :
return {
...state
}
}
}
export default reducer;
actionTypes.js actionTypes.js
export const POSTDATA="POSTDATA";
export const GETDATA = "GETDATA";
1)I am calling my action creator in callender.js file 1)我在 callender.js 文件中调用我的动作创建者
2) Where i am using thunk middleware to get data,and store in data variable from redux store 2)我在哪里使用 thunk 中间件获取数据,并存储在 redux 存储的数据变量中
3)I can't find the issue please help me 3)我找不到问题请帮助我
Your actions looks quite weird.你的行为看起来很奇怪。 The
getData
action creator disptaches fetchgetDataCall
which dispatches fetchDataApi
and that returns just some object { [data]: thunkdata}
where property data
are probably null in that moment. getData
操作创建者调度fetchgetDataCall
调度fetchDataApi
并仅返回一些 object { [data]: thunkdata}
其中属性data
可能是 null 在那一刻。 Hence there are not any properties type
or data
in your action object.因此,您的操作 object 中没有任何属性
type
或data
。
The second thing what your getData
do is returning the object {type: actionTypes.GETDATA}
, hence there is not any property data
in your action object.您的
getData
所做的第二件事是返回 object {type: actionTypes.GETDATA}
,因此您的操作 object 中没有任何属性data
。
Try to do it something like this (updated according to @mbojko answer):尝试这样做(根据@mbojko 回答更新):
const getData = () => {
return (dispatch) => {
return axios.get('http://api.openweathermap.org/data/2.5/weather?q=london,uk&appid=26aacf43db7ecfa2ecd85500eaee9920').then(thunkdata => {
return dispatch({
type: actionTypes.GETDATA,
data: thunkdata
})
})
}
}
Compare your function signature比较您的 function 签名
export const getData = (dispatch) => {
With how you call it:用你怎么称呼它:
const mapDispatchToProps = (dispatch) =>({
getWether: () => dispatch(getData())
})
The argument is missing (therefore dispatch
is not defined and obviously not a function).缺少参数(因此未定义
dispatch
,显然不是函数)。
Should be dispatch(getData(dispatch))
, probably.应该是
dispatch(getData(dispatch))
,可能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.