在Redux不工作的情况下调度操作,得到未捕获的typeError,说它不是一个函数

Dispatched action with Redux not working, getting uncaught typeError, saying it's not a function

本文关键字:函数 一个 typeError 调度 情况下 工作 Redux 操作      更新时间:2023-09-26

我有一个非常简单的动作正在调度,基本上是为了确保一切工作正常。不幸的是,我一直得到:

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:这是我在容器/test.js导入的东西

import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {testFunction} from '../actions/actions';

figure out:

我使用export default连接和导出我的组件。当只导入组件时。

我真不敢相信我做了那件事!