单击内部地图功能-Redux后隐藏按钮

时间:2019-06-20 07:59:38

标签: javascript reactjs react-redux

我有多个位置信息列表,每个位置项都有添加按钮。我想在添加操作后隐藏该“添加位置”按钮。单击该列表中的所有添加按钮

后,仅应隐藏特定的添加按钮

我已尝试使用setState并禁用了按钮状态true和false。但是它禁用了该列表中的所有按钮。我正在使用redux添加操作。我不知道如何针对唯一的位置ID

添加外观的动作:

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

添加所有位置的操作代码:

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

用于添加所有位置的归约代码:

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,
               disableButton:-1
        };
        this.togglePanel = this.togglePanel.bind(this);
        this.handleClick = this.handleClick.bind(this);
        this.allLocations = this.allLocations.bind(this);
    }
    togglePanel (e){
        this.setState({open : !this.state.open});
    }
    handleClick (mruCode){
        return this.props.addLocation(mruCode);
     }
     allLocations (){
       return this.props.addAllLocation();
    }

    componentDidMount() {
        this.props.loadData();
      }
    render(){
        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.props.location.map((loc,index)=>loc.primary===true ? (<span>{loc.mruCode} - {_labels[loc.division]} - {loc.country}</span>):null)}
                           <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.includes(item.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><ConfiguredLocation/></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())}
    }
}


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

mruCode是主要的唯一ID。单击后应隐藏特定的添加按钮。请建议我该怎么做

1 个答案:

答案 0 :(得分:1)

如果我的理解正确,您将拥有一个与位置相对应的按钮列表,并且您想隐藏已经单击的按钮。现在,您的组件还没有已添加位置的列表,因此您首先需要在mapStateToProps

中传递它
const mapStateToProps = state =>{
    return{
        location:state.locationRed.location,
        conLocations:state.conLocations
    };
};

然后在您的查看代码中,如果位置已添加,则可以删除按钮

{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>
))}