     opts: React.PropTypes.object
 
   getInitialState: ->
-    selectedIndex = @props.factory.getSelectedIndex()
-    @_createStateWithNewIndex selectedIndex
+    index = @props.factory.getSelectedIndex()
+    selected = @_isSelected index
+    {
+      selected: selected
+      initialSelected: selected
+    }
 
   componentDidMount: ->
     @props.factory.addListener @_onFactoryUpdate

  componentWillUnmount: ->
    @props.factory.removeListener @_onFactoryUpdate

  _onFactoryUpdate: (index) ->
    state = @_createStateWithNewIndex index
    @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()}>
      {
        if @state.initialSelected
          <@props.handler opts={@props.opts} />
      }
    </div>

module.exports = Panel
