如何使用ref回调将对象数组从一个组件传递到其他组件

时间:2019-06-26 10:58:49

标签: javascript reactjs redux

我有jobdetails json,用于保存位置详细信息。我必须将该位置详细信息从Jobs组件传递到我的位置组件。我已经尝试使用ref回调。同时将该位置映射到我的组件中(位置详细信息)。它显示错误(.map不是函数)。

到目前为止,我已经完成了。

工作组成部分:

import React from 'react';
import ReactDOM from 'react-dom';
import LocationPanel from '../panels/NewLocationPanel';

class JobsPanelComponent extends React.Component {

    constructor(props) {
        super(props);
        this.state = { 
            jobDetailJson: this.props.jobDetailJson

        };
this.setLocationPanelRef = cRef =>{this.locationPanel = cRef;};

}
componentWillUnmount() {
        this.clearStates();
        this.clearRefs();
        this.clearBindings();
    }
          clearStates() {

        this.state.jobDetailJson = null;
        }
        clearRefs(){
               this.locationPanel = null;
                   }
        clearBindings(){
               this.setLocationPanelRef = null;
                       }
        componentWillMount() {
        this.state.jobDetailJson = this.props.jobDetailJson;
    }

    componentWillReceiveProps(nextProps) {
        this.state.jobDetailJson = nextProps.jobDetailJson;
    }
      render(){
         var locationDataJson= null;
             if(this.state.jobDetailJson != null){
                     locationDataJson =this.state.jobDetailJson.locations;
                   }
         return(<div className="panel-group" id="jobsPanelGroup">
               <LocationPanel ref={this.setLocationPanelRef} locationDataJson={locationDataJson} title="Location"></LocationPanel></div>
              );
         }


}

LocationComponent代码:

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

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

    }

    configLocation(locationDataJson){
        let configuredList=[];
        if(locationDataJson != undefined && locationDataJson != null){
            locationDataJson.map(function(item){
               const nLoc = {...item};
                configuredList.push(nLoc);
            });
        }
        this.setState({configuredList});
        console.log(configuredList);
    }



    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.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><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,null,{withRef:true})(NewLocationPanel);

在我的位置组件中,this.props显示为空。在控制台中,它显示locationDataJson.map不是函数。我想我错过了一些东西或如何在组件中使用locationDataJson。这是JobDetailsJson中的位置结构。 locationDataJson

1 个答案:

答案 0 :(得分:1)

在您的Jobs Component中,将this.state.jobDetailJson.locations更改为this.state.jobDetailJson.locations.locationDetails,因为this.state.jobDetailJson.locations是一个对象.map仅适用于数组

var locationDataJson= null;
if(this.state.jobDetailJson != null){
   locationDataJson =this.state.jobDetailJson.locations.locationDetails;
}