   componentWillUnmount: ->
     @props.factory.removeListener @_onFactoryUpdate
 
+  _isSelected: (index) ->
+    @props.index is index
+
   _onFactoryUpdate: (index) ->
-    state = @_createStateWithNewIndex index
+    selected = @_isSelected index
+    state = selected: selected
+    if selected and !@state.initialSelected
+      state.initialSelected = true
     @setState state
-
-  _createStateWithNewIndex: (newIndex) ->
-    state = selected: newIndex is @props.index
-    if state.selected and !@state?.childComponent?
-      state.childComponent = <@props.handler opts={@props.opts} />
-    state
 
   _getStyle: ->
     display = "none"


  render: ->
    <div className={@props.className} style={@_getStyle()}>
      {@state.childComponent}
    </div>

module.exports = Panel

    opts: React.PropTypes.object

  getInitialState: ->
    index = @props.factory.getSelectedIndex()
    selected = @_isSelected index
    {
      selected: selected
      initialSelected: selected
    }

  componentDidMount: ->
    @props.factory.addListener @_onFactoryUpdate
