Redux 不起作用的调度操作,未捕获的类型错误,说它不是一个函数



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

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连接和导出我的组件。当只导入组件时。

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

相关内容

  • 没有找到相关文章

最新更新