调用操作后,Redux不会从reducer返回数据

问题描述 投票:1回答:1

我正在构建一个调用动作来获取API信息的Web应用程序。调用该操作,我可以使用console.log查看我的数组,但是,还没有调用reducer,我不知道为什么。

这里的目标是从自定义api获取货车信息并将其传回,以响应填充显示哪些货车可用的货车卡。

app.js

import React, { Component } from 'react';
import Buscard from './components/Buscard';
import DispatchHeader from './components/DispatchHeader';
import { Button } from '@material-ui/core';
import RiderTable from './components/riderTable';
import './style.css';
import './bootstrap.min.css';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom'
import withReducer from 'app/store/withReducer';
import addVan from './store/actions/add_Vans';
import getVansList from './store/actions/get_van_list';
import _ from '@lodash';
import Toast from './components/Toast';
import axios from 'axios';


const buttonStyle = {
    margin: '0 10px'
}

class AdminApp extends Component {
    constructor(props) {
        super(props);
        this.state = {
            openModal: false,
            vanName: '',
            seatsTotal: ''
        }

        this.handleNameVanChange = this.handleNameVanChange.bind(this);
        this.handleTotalSeatsChage = this.handleTotalSeatsChage.bind(this);
        this.toggleModalState = this.toggleModalState.bind(this);
        this.clearVanFormCloseModal = this.clearVanFormCloseModal.bind(this);
        this.handleVanCreation = this.handleVanCreation.bind(this);

    }
    componentDidMount() {
        this.props.getVansList();
    }

    renderVans() {
        if (this.props.vansList) {
            return this.props.vanList.map(van => {
                return <Buscard vans={van} />;
            });
        } else {
            return <div>Loading...</div>
        }
    }

    toggleModalState() {
        if (this.state.openModal) {
            this.clearVanFormCloseModal();
        } else {
            this.setState({
                openModal: true
            })
        }
    }

    handleNameVanChange(e) {
        this.setState({
            vanName: e.target.value
        });
    }

    handleTotalSeatsChage(e) {
        this.setState({
            seatsTotal: e.target.value
        });
    }

    clearVanFormCloseModal() {
        this.setState({
            vanName: '',
            seatsTotal: '',
            openModal: false
        })
    }

    handleVanCreation() {
        const vans = {
            nameVan: this.state.vanName,
            totalSeats: this.state.seatsTotal
        }
        // const nameVan = this.state.vanName;
        // const totalSeats = this.state.seatsTotal;
        this.props.addVan(vans);
        //axios.post('URL Placeholder', { nameVan, totalSeats }) 
        this.clearVanFormCloseModal();
    }

    render() {
        return (
            <div>
                {this.props.toast
                    ? <Toast
                        message={this.props.toast}
                    /> : null
                }
                <DispatchHeader
                    createVan={this.handleVanCreation}
                    open={this.state.openModal}
                    close={this.toggleModalState}
                    onVanChange={this.handleNameVanChange}
                    onTotalChange={this.handleTotalSeatsChage}
                    vanName={this.state.nameVan}
                    totalSeats={this.state.seatsTotal}
                />
                <div style={buttonStyle}>
                    <Button
                        onClick={this.toggleModalState}
                        variant="contained"
                        color="primary"
                        className="w-half"
                    >
                        ADD Van
                </Button>
                </div>
                {this.renderVans()}
                <RiderTable />
            </div>
        )
    };
}

function mapStateToProps(state) {
    return {
        vansList: state.vansList,
        toast: state.toast
    }
}

function mapDispatchToProps(dispatch) {
    return bindActionCreators({
        getVansList: getVansList,
        addVan: addVan
    }, dispatch);
}


export default connect(mapStateToProps, mapDispatchToProps)(AdminApp);

get_van_list.js //动作

import { GET_VANS_LIST } from './types';
import axios from 'axios';
import { dispatch } from 'rxjs/internal/observable/pairs';

export default function getVansList() {
    return dispatch => {
        axios.get('URL Placeholder')
            .then(res => {
                //console.log(res.data);
                const vans = res.data.map(van => {
                    //console.log(van.P_Key);
                    return van;
                });
                dispatch(getVansListAsync(vans));
            });
    }
}
function getVansListAsync(vans) {
    return {
        type: GET_VANS_LIST,
        payload: vans
    };
}

reducerVanList.js //减速器

import {
    GET_VANS_LIST,
    ADD_VANS
} from '../actions/types';


export default function (state = [], action) {

    switch (action.type) {
        case GET_VANS_LIST:
            console.log(action.payload)
            return action.payload;

        case ADD_VANS:
            return [action.payload, ...state];

        default:
            return state;
    }
}

商店配置

import * as reduxModule from 'redux';
import {applyMiddleware, compose, createStore} from 'redux';
import createReducer from './reducers';
import thunk from 'redux-thunk';

/*
Fix for Firefox redux dev tools extension
https://github.com/zalmoxisus/redux-devtools-instrument/pull/19#issuecomment-400637274
 */
reduxModule.__DO_NOT_USE__ActionTypes.REPLACE = '@@redux/INIT';

const composeEnhancers =
    process.env.NODE_ENV !== 'production' &&
    typeof window === 'object' &&
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
        window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
            // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
        }) : compose;

const enhancer = composeEnhancers(
    applyMiddleware(thunk)
);

const store = createStore(createReducer(), enhancer);

store.asyncReducers = {};

export const injectReducer = (key, reducer) => {
    if ( store.asyncReducers[key] )
    {
        return;
    }
    store.asyncReducers[key] = reducer;
    store.replaceReducer(createReducer(store.asyncReducers));
    return store;
};

export default store;
reactjs redux
1个回答
0
投票

从您的注释中看起来您没有在创建reducer时包含vansList状态对象。尝试在combineReducers中添加它。我已经在导入中假设了位置/名称,但这是个主意...

import {combineReducers} from 'redux';
import fuse from './fuse'; 
import auth from 'app/auth/store/reducers'; 
import vansList from './reducerVanList';

const createReducer = (asyncReducers) => combineReducers({ auth, fuse, vansList, ...asyncReducers }); 

export default createReducer;

这可以解释为什么没有调用reducer(我假设组件中的值未定义)。在为商店配置Reducer时,不包括state对象。

© www.soinside.com 2019 - 2024. All rights reserved.