簡體   English   中英

Redux無法執行分派操作,出現未捕獲的typeError,表示這不是函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM