0

My code

class Box extends Component {


    render () {

        return(
            <div className="box" style={styles.box}>
                <div className="img-container" style={styles}>
                    <img alt="Brand Logo" src={this.props.data.brand_logo} className="img-circle" id="brand_logo" width="50"/>
                </div>
                <ul className="box-basic-info-container">
                    <li>{basic_info_keys[0]} : {basic_info[basic_info_keys[0]]}</li>
                    <li>{basic_info[basic_info_keys[1]]}</li>
                </ul>
                {
                    iconEllipsis
                }
                <div className="box-other-info-container" >
                    <ul className="other_info" style={styles.contactInfo} >
                        {
                            other_info_keys.map(function(item,i){
                                return (<li key={i}>{item}: {other_info[item]}</li>)
                            })
                        }
                    </ul>
                </div>
                {this.renderManagerModal()}
            </div>
        )



    }
    renderManagerModal = () =>{
        return (
                <div>
                    <Modal show={this.state.showModal} onHide={this.close}>
                        <Modal.Header closeButton>
                            <Modal.Title>Managers</Modal.Title>
                        </Modal.Header>
                        <Modal.Body>
                        </Modal.Body>
                        <Modal.Footer>
                            <Button onClick={this.close}>Close</Button>
                        </Modal.Footer>
                    </Modal>
                </div>
        )



    }
}

I am new to ES6 syntax. I have used this in ES5. But this is not working in ES6.

this.renderthisManagerModal is returning an empty div

Why is it so ? How to write a function that returns a component ? I dont want to import from different file. I want to write the component in the same file .

3
  • Where is your state defined? i see your modal's appearance depends on this.state.showModal. Where do you define and update it ? Commented Aug 9, 2017 at 11:49
  • It is defined. My problem is modal is not displayed in the DOM structure when i see it in the console . Commented Aug 9, 2017 at 11:53
  • What happens if you render just some plain html inside renderManagerModal method? If it works, then it is something about your Modal Component, not Box Component. Commented Aug 9, 2017 at 11:57

3 Answers 3

1

You can do that, however I would encourage you to make use of the components in react. It's clean and you can make sure to have a separation of concerns.

I'd like to comment but my reputation is too low. Tell me, does this work?

 renderManagerModal = () => {
        return (
                <div>
                    Test
                </div>
        )
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You are using arrow function when running renderManagerModal so the lexical this is the scope of the renderManagerModal, not the modal itself. To keep this you have to use "old" function declaration function () {}

Comments

0

I think a good approach would be to put renderManagerModal in a pure component like this:

  //assuming your props are coming from the Box component
  const RenderManagerModal = (props) => {
        return (
                <div>

                    <Modal show={props.showModal} onHide={props.close}>
                        <Modal.Header closeButton>
                            <Modal.Title>Managers</Modal.Title>
                        </Modal.Header>
                        <Modal.Body>
                        </Modal.Body>
                        <Modal.Footer>
                            <Button onClick={props.close}>Close</Button>
                        </Modal.Footer>
                    </Modal>
                </div>
        )
    }

Then you would put that component inside the Box component like below:

class Box extends Component {

    showModal(){} 

    close{}

    render () {

        return(
            <div className="box" style={styles.box}>
                <div className="img-container" style={styles}>
                    <img alt="Brand Logo" src={this.props.data.brand_logo} className="img-circle" id="brand_logo" width="50"/>
                </div>
                <ul className="box-basic-info-container">
                    <li>{basic_info_keys[0]} : {basic_info[basic_info_keys[0]]}</li>
                    <li>{basic_info[basic_info_keys[1]]}</li>
                </ul>
                {
                    iconEllipsis
                }
                <div className="box-other-info-container" >
                    <ul className="other_info" style={styles.contactInfo} >
                        {
                            other_info_keys.map(function(item,i){
                                return (<li key={i}>{item}: {other_info[item]}</li>)
                            })
                        }
                    </ul>
                </div>
                <RenderManagerModal showModal={this.showModal} onHide={this.close}/>
            </div>
        )

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.