Jose Rojas
Jose Rojas

Reputation: 3520

call a same instance component multiple times ReactJS

I have next modal component in React:

class Modal extends Component {
 render(){
   return(     
    <div className="modal-content">
      <div className="modal-header">
        { this.props.modalHeader }
      </div>
      <div className="modal-body" className={this.props.className}>
        { this.props.modalBody }
      </div>
      <div className="modal-footer">
        { this.props.modalFooter }
      </div>
    </div>);
  }
}

And I'm using it this way:

 render(){

  return(
        <Modal 
           modalHeader={<ContentComponent getSection='header'/>}
           modalBody={<ContentComponent getSection='body'/>}
           modalFooter={<ContentComponent getSection='footer'/>}
        />
  );
 }

There is any way to get a reference of ContentComponent and pass this same reference into the 3 props?

I have tried reference in a constant but doesn't work.

Upvotes: 1

Views: 229

Answers (1)

HMR
HMR

Reputation: 39320

You could create a function that returns a ContentComponent

const ccWrapper = (section)=>(<ContentComponent getSection={section}/>)
//later in code
modalHeader={ccWrapper('header')}

If ContentComponent is a function it's even easier:

modalHeader={ContentComponent({getSection:'header'})}

You could also let getSection be set by Modal:

<Modal cc={ContentComponent} />
//in modal
const {cc:ContentComponent} = props;
//...
<div className="modal-header">
  <ContentComponent getSection="header" />

Upvotes: 1

Related Questions