javascript - 更新时 ComponentDidUpdate 不起作用
问题描述
我有一个包含位置详细信息的配置列表。在初始检索详细信息后,如果我删除任何位置项目或项目并更新它。它没有采用更新的值。我认为 componentdidupdate 无法正常工作。如何解决
组件代码:
export class NewLocationPanel extends React.Component{
constructor(props){
super(props);
this.state={
open:false,
configuredList:[],
clearList:[]
};
this.configLocation = this.configLocation.bind(this);
this.togglePanel = this.togglePanel.bind(this);
this.handleClick = this.handleClick.bind(this);
this.allLocations = this.allLocations.bind(this);
this.clearall = this.clearall.bind(this);
this.getLocationData = this.getLocationData.bind(this);
this.handleRemove = this.handleRemove.bind(this);
this.removeConfigLocation = this.removeConfigLocation.bind(this);
//this.removeLocationAll = this.removeLocationAll.bind(this);
}
togglePanel (e){
this.setState({open : !this.state.open});
}
handleRemove(mruCode){
this.props.removeLocation(mruCode)
}
handleClick (mruCode){
this.props.addLocation(mruCode)
}
allLocations (){
this.props.addAllLocation()
}
clearall (){
this.props.removeAllLocation()
}
componentDidMount() {
this.props.loadData();
if(this.props.locationData !=null && this.props.locationData!= undefined){
this.configLocation(this.props.locationData);
}
}
componentDidUpdate(prevProps,prevState){
console.log(prevProps);
if (prevProps.locationData != this.props.locationData) {
this.configLocation(this.props.locationData);
console.log(this.props.locationData);
}
}
configLocation(locationData){
let configuredList =[];
if(locationData.locations.locationDetails != null && locationData.locations.locationDetails !=undefined ){
configuredList = locationData.locations.locationDetails;
}
else if(locationData.locations.locationDetails === null){
configuredList = [];
}
this.setState({configuredList});
}
removeConfigLocation(index){
this.setState(({configuredList})=>{
let remList = [...configuredList]
remList.splice(index,1)
return {
configuredList:remList
}
})
}
getLocationData(){
let saveableLocationlist = [];
if(this.props.locationData === null){
saveableLocationlist = [];
}
const locationData = {
locationDetails : saveableLocationlist
}
return locationData;
}
render(){
//const{configuredList} = this.state;
const _labels = store.getLabels();
let collapsedToggle = this.props.open ? 'collapsed' : ''
return(
<div className="panel panel-default">
<div className="panel-heading" onClick={(e)=>this.togglePanel(e)}>
<div className="row">
<div className="col-xs-12 col-sm-8 col-md-6 col-lg-6 panelHeadingLabel">
<span>{this.props.title}</span>
</div>
<div className="pull-right">
<span className="defaultHeaderTextColor">{this.state.configuredList.map((loc,index)=><span key={index}>{loc.mruCode} - {_labels[loc.division]} - {loc.country}</span>)}
<span onClick={(e)=>this.togglePanel(e)} className={this.state.open ? "collapse-chevronn" : "collapse-chevron"} aria-hidden="true"></span>
</span>
</div>
</div>
</div>
{this.state.open?(
<div className="panel-body">
<div className="row grid-divider">
<div className="col-sm-6">
<div className="col-padding"><div className="pos-div"><h3>Locations List</h3><button style={{ display: this.props.location.length === this.props.conLocations.length ? "none" : "block" }} className="allLargeBtn" onClick={()=>{this.allLocations()}}>Add all locations</button></div><hr/>
{this.props.location.map((item,index)=>(
<div key={index}><div><b>{item.mruCode} - {_labels[item.division]} - {item.country}</b>{!this.props.conLocations.find(item2 => item.mruCode === item2.mruCode)&&(<div className="pull-right jd"><button style={{ display: this.state.configuredList.find(item3=> item.mruCode===item3.mruCode) ? "none" : "block" }} className="call-to-action" onClick={()=>{this.handleClick(item.mruCode)}}>Add Location</button></div>)}<hr/></div></div>))}
</div>
</div>
<div className="col-sm-6">
<div className="col-padding">
<div className="pos-div"><h3>Configured Location</h3><button className="allLargeBtn" onClick={()=>this.clearall()}>Remove all location</button></div><hr/>
<div><table className="table"><thead>{this.state.configuredList.map((locc,index)=><tr key={index}><th><b>{locc.mruCode} - {_labels[locc.division]} - {locc.country}</b></th><th className="text-right"><img alt="DeleteIcon" onClick={()=>{this.removeConfigLocation(index)}} className="deleteIconStyle" src="img/delete_large_active.png" /></th></tr>)}</thead><tbody>
{this.props.conLocations.map((loct,index)=><tr key={index}>
<td><b>{loct.mruCode} - {_labels[loct.division]} - {loct.country}</b></td>
<td className="text-right"><img alt="DeleteIcon" onClick={()=>this.handleRemove(loct.mruCode)}className="deleteIconStyle" src="img/delete_large_active.png" /></td>
</tr>
)}
</tbody></table></div>
</div>
</div>
</div>
</div>):null}
</div>
);
}
}
const mapStateToProps = state =>{
return{
location:state.locationRed.location,
conLocations:state.locationRed.conLocations
};
};
const mapDispatchToProps = (dispatch) => {
return{
loadData:()=>{dispatch(loadData())},
addLocation:(mruCode)=>{dispatch(addLocation(mruCode))},
addAllLocation:() =>{dispatch(addAllLocation())},
removeLocation: (mruCode)=>{dispatch(removeLocation(mruCode))},
removeAllLocation: () =>{dispatch(removeAllLocation())}
}
}
export default connect(mapStateToProps,mapDispatchToProps,null,{withRef:true})(NewLocationPanel);
在 componentDidUpdate 方法中检查 console.log(this.props.locationData) 时,最初它是给出列表项。删除任何项目后,如果我保存更改,那么它也会提供与已删除项目相同的列表项目。如何使它工作?
解决方案
看起来您并没有更新this.props.locationData
in removeConfigLocation
(或除初始 之外的任何地方GET
)。老实说,看起来locationData
应该将您利用的方式放入state
而不是props
.
您还应该注意,您在以下几行中破坏了您的状态:
// before: this.state={ open:false, configuredList:[], clearList:[] };
this.setState(({configuredList})=>{
let remList = [...configuredList]
remList.splice(index,1)
return {
configuredList:remList
}
})
// after: this.state={ configuredList: remList};
如果您想在更新后保持可用,您应该确保传播( )。...this.state
推荐阅读
- java - 将显示 txt 文件的学生信息系统
- swiftui - SwiftUI:当视图不存在时更新 ui 会导致“无法呈现视图。请提交错误。”
- javascript - 在 send() 调用中的“options”对象中不包括“gas”或“gasPrice”属性
- authorization - 为什么我的用户未经授权使用 aws-amplify api 和 cognito 身份验证执行 graphql 突变?
- ruby-on-rails - 验证失败:发件人必须存在,收件人必须存在
- kubernetes - 将非 Azure VM 附加到 Azure Kubernetes 服务 (AKS)
- c++ - 使用类头对函数的未定义引用
- json - Android Retrofit - 数组数据为空
- python - wxPython中如何使仪表跟随线程中的下载功能?
- javascript - “else”语句在更新 Firebase 用户名时不断触发?