gpt4 book ai didi

javascript - 当所有项目添加到数组 -redux 时隐藏按钮

转载 作者:行者123 更新时间:2023-12-02 23:24:33 26 4
gpt4 key购买 nike

我的位置列表有多个位置。有一个“添加全部按钮”。当我单击该按钮时,所有位置都将添加到另一个数组中。现在,添加操作工作正常。但我需要在单击操作后隐藏“添加全部按钮”。

我为此创建了操作和 reducer 。到目前为止我已经完成了。

操作代码:(适用于所有位置和单个位置)

export const addAllLocation = () =>({
type : ALL_LOCATION
});

export const addLocation = mruCode =>({
type: ADD_LOCATION,
payload:mruCode
});

reducer 代码:(适用于所有位置和单个位置)

case 'ALL_LOCATION':
return{
...state,
conLocations:[...state.location]
}

case 'ADD_LOCATION':
let addedLoc = state.location.find(obj=>(obj.mruCode === action.payload))
return{
...state,
conLocations: [...state.conLocations,addedLoc]
};

组件代码:

export class NewLocationPanel extends React.Component{
constructor(props){
super(props);
this.state={
open:false,
conLocations:[]
//configuredList:[]
};
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);
}
togglePanel (e){
this.setState({open : !this.state.open});
}
handleClick (mruCode){
this.props.addLocation(mruCode)
}
allLocations (){
this.props.addAllLocation()
}
clearall (){
this.props.removeAllLocation()
}

componentDidMount() {
this.props.loadData();
}

componentDidUpdate(prevProps){
if ((prevProps.jobId != this.props.jobId || prevProps.jobDetailJson != this.props.jobDetailJson) && this.props.jobDetailJson != undefined) {
this.configLocation(this.props.jobDetailJson);
}

}

configLocation(jobDetailJson){
let conLocations = jobDetailJson.locations.locationDetails;
this.setState({conLocations});
console.log(conLocations);
}

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.conLocations.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 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 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>{this.state.conLocations.map((locc,index)=><table className="table" key={index}><tbody><tr><td><b>{locc.mruCode} - {_labels[locc.division]} - {locc.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><ConfiguredLocation/></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())},
removeAllLocation: () =>{dispatch(removeAllLocation())}
}
}


export default connect(mapStateToProps,mapDispatchToProps,null,{withRef:true})(NewLocationPanel);

单击后,添加全部按钮将不可见。如何将该 mruCode 映射到 onClick 操作中,以便基于此我可以隐藏按钮

最佳答案

您可以执行以下操作:

style={{ display: this.props.locations.length === this.props.conLocations.length ? "none" : "block" }}

通过将其添加到按钮上,每次状态更改并使用react重新渲染时,该值都会在“无”和“阻止”之间变化。

如下所示:

<button style={{ display: this.props.locations.length === this.props.conLocations.length ? "none" : "block" }} className="allLargeBtn" onClick={()=>{this.allLocations()}}>**Add all locations**</button>

关于javascript - 当所有项目添加到数组 -redux 时隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56793664/

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