【问题标题】:My Redux state is getting changed, but it doesn't re-render我的 Redux 状态正在改变,但它不会重新渲染
【发布时间】:2018-07-18 20:22:35
【问题描述】:

我有一个父组件和一个子组件。我可以看到减速器正在更新(请参阅屏幕截图),但它没有重新渲染组件。我正在调用一些 API,一旦响应到来,它只会更新一些变量并重新渲染组件。

父组件

import React, { Component } from 'react';
import { Tab, Tabs, TabList, TabPanel } from 'react-tabs';
import {connect} from "react-redux";
import { bindActionCreators } from 'redux'
import ProfileInfo from '../components/Profile/ProfileInfo'
import Details from '../components/Profile/Details'
import UnitSpec from '../components/Profile/UnitSpec'
import * as profileActionCreators from "./../actions/profileActions";
import * as StaticData from '../utils/country'


class NewProfilePage extends Component {
  constructor(props) {
    super(props);
    this.state = { tabIndex: 0 ,
    	countryList: StaticData.Country,

    };
    let { dispatch } = this.props
    this.actions = bindActionCreators({...profileActionCreators} , dispatch);
  }

  componentWillReceiveProps(nextProps) {
  	console.log('nextProps', nextProps)
  }

  render() {
    return (
        <div className="admin-holder">
            <div className="row profile-row">

                <div className="small-12 large-4 columns">
                    <ProfileInfo></ProfileInfo>
                </div>
                <div className="small-12 large-8 columns">
                		<Tabs selectedIndex={this.state.tabIndex} onSelect={tabIndex => this.setState({ tabIndex })}>
					        <TabList>
					          <Tab>Details</Tab>
					          <Tab>Unit Spec</Tab>
					          <Tab>Imaginery</Tab>
					          <Tab>Menu</Tab>
					        </TabList>
						        <TabPanel> 
     								<Details updateDetails = {(userDetail) => this.actions.updateDetails(userDetail)} countryList={this.state.countryList}/>
						        </TabPanel>
						        <TabPanel> 
						        	<UnitSpec staticData={StaticData}/>
						        </TabPanel>
						        <TabPanel> 
						        </TabPanel>
						        <TabPanel> 
						        </TabPanel>
					    </Tabs>
				</div>
			</div>

		 </div>
    );
  }
}

const mapStateToProps= (state) => {
	console.log('state', state)
	return{
	     profileReducer: state.profileReducer
	};
};



export default connect(mapStateToProps)(NewProfilePage);

减速器

import * as types from '../constant';


const initialCommonState = {
  	countryList: [],
  	loadingCountryList: false,
  	isProfileUpdated: false
}




const profileReducer = (state=initialCommonState, action) => {
	 switch(action.type){
        case types.UPDATE_DETAILS + "_FULFILLED":
      			const response = action.payload;
      			return Object.assign({}, state, {
                    isProfileUpdated: true
                  });

        case types.UPDATE_DETAILS + "_PENDING":
            return Object.assign({}, state, {
                    isProfileUpdated: false
                  });

        case types.UPDATE_DETAILS + "_REJECTED":
            return Object.assign({}, state, {
                    isProfileUpdated: false
                  });

        default : 
        	return state    
	}

}

export default profileReducer;

请同时查看屏幕截图,您可以看到“isProfileUpdated”已更改为 true

商店

import {createStore,combineReducers,applyMiddleware} from 'redux'
import logger from 'redux-logger';
import thunk from 'redux-thunk'
import promise from 'redux-promise-middleware'
import reducers from '../reducers';

const middleware = [];
if (process.env.NODE_ENV === 'development') {
    middleware.push(logger);
}

export default createStore(
    combineReducers({reducers}),
    {},
    //applyMiddleware(logger(),thunk , promise())
    applyMiddleware(logger, thunk , promise())
)

【问题讨论】:

  • 正如答案所暗示的,通常最好也包括您的商店设置;你可能没有使用对象速记。您还可以在 mapStateToProps 中放置日志或断点作为健全性检查。

标签: reactjs redux react-redux redux-thunk


【解决方案1】:

您的状态包含个人资料

const mapStateToProps= (state) => {
    console.log('state', state)
    return{
         profileReducer: state.profile
    };
};

【讨论】:

    猜你喜欢
    • 2018-12-31
    • 2020-01-20
    • 1970-01-01
    • 2020-07-26
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    相关资源
    最近更新 更多