简体   繁体   English

在redux中更新了状态,以接收新道具,但是由于未重新渲染组件而未在componentWillReceiveProps中设置状态

[英]State updated in redux receiving new props but state is not setting in componentWillReceiveProps due to which component doesn't re-render

I'm using a modal to add new object to an array. 我正在使用模式将新对象添加到数组。 Everything goes well, I update the state in reducer. 一切顺利,我更新了reducer中的状态。 Even redux logger also shows that the object has been added. 甚至redux logger也显示该对象已添加。 Even in the componentWillReceiveProps lifecycle I'm getting the new props but the local state doesn't gets set inside this lifecycle hook due to which component doesn't re-render. 即使在componentWillReceiveProps生命周期中,我也正在获取新的道具,但是由于哪个组件未重新渲染,因此未在此生命周期挂钩中设置本地状态。

I have updated the state in reducer. 我已经更新了减速器的状态。 Even I'm getting the new props in my component. 甚至在我的组件中也有新的道具。

This is the component which needs to re-render 这是需要重新渲染的组件

import React, { Component } from 'react';
import ReactTable from 'react-table';
import '../../../css/listSalaryRange.css';
import NumberEditor from '../../../components/reactTable/numberEditor';
import i18n from '../../../i18n';

class ListSalaryRange extends Component {
constructor(props) {
    super(props);
    this.state = {
        dataList: this.props.salaryRange,
    }
    this.renderNumberEditor = this.renderNumberEditor.bind(this);
 }

componentWillReceiveProps = (nextProps) => {
    debugger;
    if (this.props.salaryRange !== nextProps.salaryRange) {
        this.setState({ dataList: nextProps.salaryRange });
    }
}

renderNumberEditor = (cellInfo) => {
    // debugger;
    // console.log('cell Info', cellInfo);
    return (
        <NumberEditor minValue={0}
            entityRow={cellInfo.original}
            width={cellInfo.width} allowDecimal={false}
            value={cellInfo.value} id={cellInfo.row.SalaryRangeId + '-' + cellInfo.column.id}
            entityId={cellInfo.row.SalaryRangeId} fieldName={cellInfo.column.id} maxLength={3}
            // onUpdate={this.onUpdate}
            className={'v-rt-input'} />
    )
}

render() {
    const dataList = this.state.dataList;
    const style = { textAlign: 'left', overflow: 'inherit', whiteSpace: 'nowrap' };
    const tableColumn = [
        {
            Header: 'Comp Range',
            accessor: 'SalaryRangeId',
            className: 'v-rt-readonly-cell',
            style: { style },
        },
        {
            Header: 'Minimum Compensation',
            accessor: 'MinSalary',
            style: { style },
            Cell: props => this.renderNumberEditor(props),
        },
        {
            Header: 'Maximum Compensation',
            accessor: 'MaxSalary',
            className: 'v-rt-readonly-cell',
            style: { style },
        },
        {
            Header: 'Average Compensation',
            accessor: 'AvgSalary',
            className: 'v-rt-readonly-cell',
            style: { style },
        },
        {
            Header: 'Load Factor',
            accessor: 'LoadFactor',
            style: { style },
            Cell: props => this.renderNumberEditor(props),
        },
        {
            id: 'DeleteSalaryRange',
            accessor: e => {
                return (
                    <button type="button" className="left mgL6 v-btn v-btn-sm v-btn-neutral-solid mgT8">
                        <div className="left wd35 pdT5 center-text" style={{ marginTop: '-7px' }}><i className="ion-ios-trash" aria-hidden="true"></i></div>
                        <div className="left pdR10 col-phone-hidden"><span>Delete</span></div>
                    </button>
                )
            }
        }
    ];

    return (
        <div id="salaryListContainer">
            <ReactTable
                data={dataList}
                columns={tableColumn}
                defaultPageSize={dataList.length}
                showPagination={false}
                resizable={false}
            />
        </div>
    )

}

}

export default ListSalaryRange;

This is the connected parent component 这是连接的父组件

import React, { Component } from 'react';
import { connect } from 'react-redux';
import Header from './header';
import ListSalaryRange from './listSalaryRange';
import { bindActionCreators } from 'redux';
import { addCompSalaryRange } from '../../../actions/adminActions'
import AppConfig from '../../../appConfig';
import Notifications from '../../../components/common/notifications';
import ShowLoadingNotificationMVC from 
'../../../components/common/showLoadingNotificationMVC';

class Index extends Component {
// componentDidMount() {
//     document.getElementById('compRangesNotification').className = '';
// }
render() {
    return (
        <div>
            <Header salaryRange={this.props.salaryRange} 
addCompSalaryRange={this.props.addCompSalaryRange}/>
            <ListSalaryRange salaryRange={this.props.salaryRange} />
            {/* <div className='v-notify'>
                <ShowLoadingNotificationMVC id={'compRangesNotification'} 
message={'Loading'} />
                <Notifications />
            </div>
            <div className="compRanges-content">
                <iframe id="ifrmCompRanges" height={1650} title={''} 
style={{ border: 'none' }} width={'100%'} src={AppConfig.baseUrl + ' 
ViciAdmin/SalaryRanges2'} />
            </div> */}
        </div>
    )
}
}

const mapStateToProps = (state) => ({
filter: state.filter,
salaryRange: Object.values(state.masterData.salaryRange),
});

const mapDispatchToProps = (dispatch) => {
return bindActionCreators({
    addCompSalaryRange
}, dispatch);
};

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

I want to re-render ListSalaryRange component 我想重新渲染ListSalaryRange组件

this is why your table doesn't update 这就是为什么您的表不更新的原因

const dataList = this.state.dataList;

instead you should do: 相反,您应该这样做:

const {dataList} = this.state

one more tip: you don't need to use state in that case, you could directly use this.props.salaryRange. 另一个提示:在这种情况下,您不需要使用状态,可以直接使用this.props.salaryRange。 it should save you some line of codes, cause you don't have to declare state, and you don't have to use componentWillReceiveprops to update your state.. it would update directly 它应该为您节省一些代码,因为您不必声明状态,也不必使用componentWillReceiveprops来更新状态。它将直接更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM