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

标签 javascript reactjs react-redux

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

我为此创建了操作和 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/

相关文章:

javascript - getelementbyid 不工作

javascript - pikaday 插件 两个日期之间 15 天

javascript - 切换表格单元格内的数据显示

reactjs - 如何在 React 组件中测试 Redux 的 dispatch() 是否被调用(Jest + Enzyme)

javascript - 使用 jquery 或 javascript 通过 AJAX 读取嵌套的 JSON 并输出到表

javascript - 在 react 中滚动视频

javascript - 每当我在 React 上运行 npm start 时,都会出现错误。下图

reactjs - 我的代码无法更新嵌套组件

javascript - 流: Throws error Cannot resolve module "react-redux" even tho it's installed

reactjs - React 组件中的 JSX 将西里尔文文本显示为 habra-codabra