【问题标题】:Redux-thunk dispatch function doesn't work on LaravelRedux-thunk 调度功能在 Laravel 上不起作用
【发布时间】:2019-10-12 21:45:11
【问题描述】:

我在我的项目中使用 React-Laravel。 问题是当我尝试将 redux-thunk 用于异步调度功能时。 我的调度功能不会被执行。 请帮我解决这个问题。

我已经尝试过使用 promise 或 redux-devtools-extension 库 https://codeburst.io/reactjs-app-with-laravel-restful-api-endpoint-part-2-aef12fe6db02

app.js

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';

import Layout from './jsx/Layout/Layout';
import marketplaceReducer from './store/reducers/marketplace';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const appReducer = combineReducers({
    marketplace: marketplaceReducer
});

const rootReducer = (state, action) => {
    return appReducer(state, action);
}

const store = createStore(rootReducer, composeEnhancers(
    applyMiddleware(logger, thunk)
));

const render = (
    <Provider store={store}>
        <BrowserRouter>
            <Layout />
        </BrowserRouter>
    </Provider>
);

ReactDOM.render(render, document.getElementById('root'));

ma​​rketplace.js(动作)

import * as actionTypes from './actionTypes';
import axios from '../../axios';

export const loadMarketplace = () => {
    console.log("Load Marketplace");
    return {
        type: actionTypes.LOAD_MARKETPLACE
    };
}

export const successMarketplace = (data) => {
    console.log("Success Marketplace");
    return {
        type: actionTypes.SUCCESS_MARKETPLACE,
        data: data
    }
}

export const failedMarketplace = () => {
    console.log("Failed Marketplace");
    return {
        type: actionTypes.FAILED_MARKETPLACE
    }
}

export const showMarketplace = () => {
    console.log("Show Marketplace Action")
    return dispatch => {
        //This is the problem
        //Inside this function, I can't see any console.log, even loadMarketplace() didn't get called.
        console.log("Show Marketplace in dispatch");
        dispatch(loadMarketplace());
        axios.get('/marketplaces')
            .then(response => {
                dispatch(successMarketplace(response));
            })
            .catch(error => {
                dispatch(failedMarketplace());
            });
    };
}

ma​​rketplace.js(减速器)

import * as actionTypes from '../actions/actionTypes';

const initial_state = {
    data: [],
    loading: false
}

const loadMarketplace = (state, action) => {
    console.log("Load Marketplace Reducer")
    return {
        ...state,
        loading: true
    };
}
const successMarketplace = (state, action) => {
    console.log("Success Marketplace Reducer", action.data)
    return {
        ...state,
        loading: false,
        data: action.data
    };
}

const failedMarketplace = (state, action) => {
    return {
        ...state,
        loading: false
    };
}

const reducer = (state = initial_state, action) => {
    //This is called when the first init, never got it through showMarketplace() function.
    console.log("Marketplace Reducer", action);
    switch (action.type) {
        case actionTypes.LOAD_MARKETPLACE: return loadMarketplace(state, action);
        case actionTypes.SUCCESS_MARKETPLACE: return successMarketplace(state, action);
        case actionTypes.FAILED_MARKETPLACE: return failedMarketplace(state, action);
        default: return state;
    }
}

export default reducer;

Marketplace.js(jsx 视图)

import React, { Component } from 'react';
import { connect } from 'react-redux';

import * as actions from '../../../store/actions';

class Marketplace extends Component {
    componentDidMount() {
        console.log('[ComponentDidMount] Marketplace')
        this.props.showMarketplace();
    }

    render() {
        return (
            <React.Fragment>
                Marketplace
            </React.Fragment>
        );        
    }
}

const mapDispatchToProps = dispatch => {
    return {
        showMarketplace: () => dispatch(actions.showMarketplace)
    };
}


export default connect(null, mapDispatchToProps)(Marketplace);

这是我的console.log的结果(当Marketplace.js第一次加载时)

请帮忙,我已经挣扎了 2 个小时或更长时间,只是因为这个问题。 (这是我第一次使用 React-Laravel)。 谢谢。

【问题讨论】:

    标签: javascript reactjs redux redux-thunk react-laravel


    【解决方案1】:

    已编辑:我认为这是关于 thunk 未添加到 redux 的问题。

    首先尝试只添加thunk。

    const store = createStore(rootReducer, composeEnhancers(
        applyMiddleware(thunk)
    ));
    

    如果可行,不妨尝试更改它们的顺序。

    【讨论】:

    • 实际上,我想查看我的response 变量,所以我就这样保留它,但我什至无法获得“加载市场”console.log。 (这个只是应用加载动作)。你可以看到我的 console.log 图像,我只得到了 Show Marketplace Action 日志。然后是函数的记录器。
    • 我已经找到了问题,对于redux用户来说这是一个初学者的错误。顺便说一句,感谢您的帮助!
    • 出了什么问题?我没注意到。
    【解决方案2】:

    我已经发现了问题。这不是redux-thunk 问题。 这实际上是我们在任何地方发现的normal Redux 问题。

    Marketplace.js(jsx 视图)

    import React, { Component } from 'react';
    import { connect } from 'react-redux';
    
    import * as actions from '../../../store/actions';
    
    class Marketplace extends Component {
        componentDidMount() {
            console.log('[ComponentDidMount] Marketplace')
            this.props.showMarketplace();
        }
    
        render() {
            return (
                <React.Fragment>
                    Marketplace
                </React.Fragment>
            );        
        }
    }
    
    const mapDispatchToProps = dispatch => {
        return {
            showMarketplace: () => dispatch(actions.showMarketplace) //THIS IS THE PROBLEM, IT IS NOT EXECUTING PROPERLY. THIS ONE SHOULD BE
            showMarketplace: () => dispatch(actions.showMarketplace()) //SHOULD BE LIKE THIS.
        };
    }
    
    
    export default connect(null, mapDispatchToProps)(Marketplace);
    

    【讨论】:

      猜你喜欢
      • 2017-05-25
      • 2019-11-12
      • 2018-05-30
      • 2020-07-11
      • 2016-11-23
      • 2018-05-21
      • 2019-11-12
      • 2020-08-18
      • 2021-04-05
      相关资源
      最近更新 更多