gpt4 book ai didi

javascript - React-Redux 中的页面加载时未调度操作

转载 作者:行者123 更新时间:2023-12-03 02:32:18 24 4
gpt4 key购买 nike

我在页面加载时调用我的操作“GetMobileList”。但是在执行该操作之后,不会像mapstatestoprops或任何reducer那样调用任何东西。它们在调用操作之前被调用,而在调用操作之后什么也没有发生。我在页面加载时绑定(bind)一个列表。操作完成后,我通过父组件的 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 成为一个 prop。

因此,不要调用 GetMobileList(),而是尝试 this.props.GetMobileList()

关于javascript - React-Redux 中的页面加载时未调度操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48685611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com