0

I am using a react component react-awesome-modal as follows.

<Modal visible={this.state.visible}  width="850" height="450" effect="fadeInUp" onClickAway={() => this.closeModal()}>

Now I want to add a new style overflow-y:scroll to this Modal component.

I tried doing this, but it didn't worked:

<Modal visible={this.state.visible}  style={{"overflow-y":"scroll"}} width="850" height="450" effect="fadeInUp" onClickAway={() => this.closeModal()}>

Is there any way I can apply the property to this component.

PS: I had tried applying this property using chrome Devtool(Inspect element). In that, I am able to see the complete CSS that is used for this component, so I was able to update it easily (see the last line in the image)

enter image description here

5
  • did you try without the curly brackets? style="overflow-y":"scroll". Commented Apr 7, 2020 at 19:29
  • Getting error Identifier expected. brackets are necessary. Commented Apr 7, 2020 at 19:32
  • You could try use ref to get the DOM of component <Modal>, then change the style of the DOM. Commented Apr 7, 2020 at 19:40
  • Fyi this component is deprecated: shibe97.github.io/react-awesome-modal. Also, looks like <Modal>'s implementation does not render your style prop. Commented Apr 7, 2020 at 19:41
  • 1
    @Kuo-hsuanHsu Can you please throw light on how to get the DOM of any component. Commented Apr 7, 2020 at 19:47

1 Answer 1

2

You could try insert a div element under <Modal /> , then set the style of this div:

<Modal visible={this.state.visible} width="400" height="300" effect="fadeInUp" onClickAway={() => this.closeModal()}>
  <div style={{overflowY:"scroll", height:"300px"}}>
      <h1>Title</h1>
      <p>Some Contents</p>
      <a href="javascript:void(0);" onClick={() => this.closeModal()}>Close</a>
  </div>
</Modal>

If solution above doesn't work, then try creatRef, get DOM of <Modal />, and change the style of <Modal />: (React version must be newer than 16.3)

import React, { Component } from 'react';
import Modal from 'react-awesome-modal';

class Test extends React.Component {
   constructor(props) {
       super(props)
       this.modalRef = React.createRef()  //create Ref to get DOM
   }

  componentDidMount(){
       this.modalRef.current.style.overflowY = "scroll"   //change style of Modal DOM
  }

   render() {

       return (
          <div>
             <Modal visible={this.state.visible} width="400" height="300" effect="fadeInUp" onClickAway={() => this.closeModal()} ref={this.modalRef}>
                <div>
                    <h1>Title</h1>
                    <p>Some Contents</p>
                    <a href="javascript:void(0);" onClick={() => this.closeModal()}>Close</a>
                </div>
             </Modal>
          </div>
       )
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The first solution worked like a charm. Didn't tried the second approach. Thanks a lot!!!

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.