2

I am making a React app and using http://marcio.github.io/react-skylight/ to implement modals.

export default class SomeComponent extends Component {
  ...
  render() {
    return (
      ...
        <SkyLight ref="improveTaskModal">
          <form>
            <input type="text" ref="myInput" />
      ...
    );
  }
}

I want to focus on the input element as soon as the modal opens up. So, I tried doing

_executeAfterModalOpen(){
     this.refs.myInput.focus();
}

But, here this refers to

Object { 
  hideOnOverlayClicked: true, 
  afterOpen: wrapMethod/<(),
  dialogStyles: Object,
  title: "Improve task", 
  children: Object,
  showOverlay: true,
  overlayStyles: Object,
  closeButtonStyle: Object
}
edi9999
  • 19,701
  • 13
  • 88
  • 127
purezen
  • 612
  • 12
  • 29
  • 2
    I may have misunderstood the question, but how/where exactly is the function "_executeAfterModalOpen" located within the overall reactjs code? – Aaron Feb 29 '16 at 08:34

2 Answers2

2

It doesn't look like you are binding the component's this reference to _executeAfterModalOpen.

Put

this._executeAfterModalOpen = this._executeAfterModalOpen.bind(this)

in your component constructor.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
syousif
  • 76
  • 1
  • 4
0

@syousif solution is correct. you need to bind the function with the context of the class. Also other than that, If you want to focus as soon as your modal loads then you have to do this in componentDidMount, so instead of the function you can do like this:

componentDidMount(){this.refs.myInput.focus();}

and you don't need to bind this function as it is Component class function.

Minkesh Jain
  • 1,140
  • 1
  • 10
  • 24