我在页面加载时将动作称为“ GetMobileList”。但是执行该操作后,不会再调用mapstatestoprops或任何reducer之类的东西。它们在调用操作之前被调用,而在调用操作之后则什么也没有发生。我在页面加载时绑定列表。完成操作后,我通过父组件的props渲染列表组件。



import React, { Component } from 'react';
import GetProductsList from './list'
import GetMobileList from '../actions/index'
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';

class Add extends Component
{
    constructor (props) {
        super(props);
    }
    componentDidMount()
    {
        GetMobileList();
    }
    render(){
        return (
            <div>
                <GetProductsList mobilesList={this.props.mobiles}/>
            </div>
        )
    }
}

function mapStateToProps (state) {
    return {
        mobiles: state.mobiles
    }
}

export default connect (mapStateToProps,{GetMobileList}) (Add)
......................................................

export default  function GetMobileList()
{
    var mobiles= JSON.parse(localStorage.getItem('mobiles'));
    return  {
        type:'MOBILE_LIST',
        payload :  mobiles
    };
}

Index.js for Reducer

import {combineReducers} from 'redux';
import MobilesList from './mobileslist';

const rootreducer = combineReducers({
    mobiles:MobilesList
});

export default rootreducer;

REDUCER -

export default function (state=null,action)
{
    switch(action.type)
    {
        case 'MOBILE_LIST':
        {
            return action.payload
        }
    }
    return state;
}

最佳答案

matchDispatchToProps中,GetMobileList成为道具。

因此,请尝试使用GetMobileList()而不是this.props.GetMobileList()

10-01 11:55