![](/img/trans.png)
[英]Redux Uncaught type error: dispatched action is not a function
[英]Dispatched action with Redux not working, getting uncaught typeError, saying it's not a function
我將分派一個非常簡單的操作,基本上是為了確保一切正常。 不幸的是我不斷得到:
Uncaught TypeError: _this2.props.testFunction is not a function
我檢查了所有內容,一切似乎都很好。 這是我的代碼:
//index.js
const loggerMiddleware = createLogger();
const middleware = applyMiddleware(thunkMiddleware, loggerMiddleware);
const store = createStore(allReducers, middleware);
export default class Help2 extends Component{
render(){
return (
<Provider store={store}>
<App/>
</Provider>
);
}
}
//reducers/index.js
const allReducers = combineReducers({
topic: TopicsReducer
});
export default allReducers;
//reducer/test-reducers.js
export default function(state=null, action){
switch(action.type){
case 'TEST_REDUCER':
return action.payload; //will return that object
break;
}
return state;
}
//actions.js
export function testFunction(topicsUrl){
console.log('I have been called');
const request= axios.get(topicsUrl);
return (dispatch)=>{
request.then(({data})=>{
dispatch({type: 'TEST_REDUCER', payload: data,});
});
}
};
//container/test.js
export class Test extends Component{
render(){
return(
<div onClick={()=>this.props.testFunction("https://api.someApi.com/")}>Click here to test</div>
);
}
}
function mapStateToProps(state){
return{
topic: state.topic
}
}
function matchDispatchToProps(dispatch){
return bindActionCreators({testFunction: testFunction}, dispatch);
}
export default connect(mapStateToProps, matchDispatchToProps)(Test);
如此,有人知道可能出什么問題了嗎?
編輯:修復了動作類型
Edit2:這是我在container / test.js中導入的東西
import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {testFunction} from '../actions/actions';
弄清楚了:
我在使用export default進行連接和導出組件。 同時僅導入組件。
我不敢相信我做到了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.