2017-06-13 69 views
1

我在組件中使用了選項卡結構,這兩個選項卡都具有相同的組件。ReactJs:爲具有相同組件的選項卡保持獨立狀態

如果點擊添加按鈕,一個新的組件被添加到選項卡,現在如果我切換選項卡並轉到選項卡2,它也有相同的組件。

我試圖在下面的小提琴中複製場景。

https://jsfiddle.net/rzv6Lrjh/89/

var App = React.createClass({ 
    render: function() { 
    return (
     <div> 
     <Tabs selected={0}> 
      <Pane label="Tab 1"> 
      <Tickets/> 
      </Pane> 
      <Pane label="Tab 2"> 
      <Tickets/> 
      </Pane> 
     </Tabs> 
     </div> 
    ); 
    } 
}); 
+0

並不一定要了解你想要什麼完整的答案,但我想你應該考慮派遣「ticketlist」作爲Tickets組件的一個屬性並在頂級組件級別狀態下維護2個不同的列表 –

+0

@AshKander可以請您看看我添加的小提琴,問題是兩個選項卡都具有相同的組件,所以如果我在選項卡1中對組件進行一些更改,同樣在標籤2中反映出來。 – asdfdefsad

回答

3

好了,所以你只需要做出反應過來了解您的2種門票成分是不同的。

可以簡單地通過添加僞屬性給此組件

<Tickets key="1/> 

第一個選項卡和實現這一

<Tickets key="2/> 

爲第二個。然後,你將有像您期望的2渲染;)


傳遞門票道具

var Tabs = React.createClass({ 
    displayName: 'Tabs', 
    propTypes: { 
    selected: React.PropTypes.number, 
    children: React.PropTypes.oneOfType([ 
     React.PropTypes.array, 
     React.PropTypes.element 
    ]).isRequired 
    }, 
    getDefaultProps: function() { 
    return { 
     selected: 0 
    }; 
    }, 
    getInitialState: function() { 
    return { 
     selected: this.props.selected 
    }; 
    }, 
    shouldComponentUpdate(nextProps, nextState) { 
    return this.props !== nextProps || this.state !== nextState; 
    }, 
    handleClick: function (index, event) { 
    event.preventDefault(); 
    this.setState({ 
     selected: index 
    }); 
    }, 
    _renderTitles: function() { 
    function labels(child, index) { 
     var activeClass = (this.state.selected === index ? 'active' : ''); 
     return (
     <li key={index}> 
      <a href="#" 
      className={activeClass} 
      onClick={this.handleClick.bind(this, index)}> 
      {child.props.label} 
      </a> 
     </li> 
    ); 
    } 
    return (
     <ul className="tabs__labels"> 
     {this.props.children.map(labels.bind(this))} 
     </ul> 
    ); 
    }, 
    _renderContent: function() { 
    return (
     <div className="tabs__content"> 
      {this.props.children[this.state.selected]} 
     </div> 
    ); 
    }, 
    render: function() { 
    return (
     <div className="tabs"> 
     {this._renderTitles()} 
     {this._renderContent()} 
     </div> 
    ); 
    } 
}); 

var Pane = React.createClass({ 
    displayName: 'Pane', 
    propTypes: { 
    label: React.PropTypes.string.isRequired, 
    children: React.PropTypes.element.isRequired 
    }, 
    render: function() { 
    return (
     <div> 
     {this.props.children} 
     </div> 
    ); 
    } 
}); 

var CreateTicket = React.createClass({ 
    getInitialState: function() { 
    return{}; 
    }, 
    render: function() { 
    return(
     <button type="button" onClick={this.props.createTicket} className="add-another-ticket"> 
     +Add Ticket 
     </button> 
    ); 
    } 
}); 

var Tickets = React.createClass({ 
    render: function() { 
    return (
     <div> 
     {this.props.tickets} 
     <CreateTicket createTicket={this.props.onCreateTicket} /> 
     </div> 
    ); 
    } 
}); 

var IndividualTicketInput = React.createClass({ 
    getInitialState: function() { 
    return { ticket: {name: '', quantity: '', price: null} }; 
    }, 
    render: function() { 
    return (
     <ul> 
     <li> 
      <label>Ticket Name</label> 
      <input className="ticket-name" type="text" placeholder="E.g. General Admission" value={this.state.ticket.name} /> 
     </li> 
     <li> 
      <label>Quantity Available</label> 
      <input className="quantity" type="number" placeholder="100" value={this.state.ticket.quantity} /> 
     </li> 
      <li> 
      <label>Price</label> 
      <input className="price" type="number" placeholder="25.00" value={this.state.ticket.price} /> 
      </li> 
     <li> 
      <button type="button" className="delete-ticket" onClick={this.deleteTicket}><i className="fa fa-trash-o delete-ticket"></i></button> 
     </li> 
     </ul> 
    ); 
    } 
}); 

var App = React.createClass({ 
    getInitialState: function() { 
     return { 
      tickets1: [(<IndividualTicketInput/>)], 
      tickets2: [(<IndividualTicketInput/>)] 
     } 
    }, 
    onCreateTicket1: function() { 
     this.state.tickets1.push(<IndividualTicketInput/>); 
    }, 
    onCreateTicket2: function() { 
     this.state.tickets2.push(<IndividualTicketInput/>); 
    }, 
    render: function() { 
     return (
     <div> 
      <Tabs selected={0}> 
       <Pane label="Tab 1"> 
        <Tickets tickets={this.state.tickets1} onCreateTicket={this.onCreateTicket1.bind(this)}/> 
       </Pane> 
       <Pane label="Tab 2"> 
        <Tickets tickets={this.state.tickets2} onCreateTicket={this.onCreateTicket2.bind(this)}/> 
       </Pane> 
      </Tabs> 
     </div> 
     ); 
    } 
}); 

ReactDOM.render(<App />, document.querySelector('.container')); 
+0

感謝它的工作。一個小問題,https://jsfiddle.net/rzv6Lrjh/90/。如果我點擊添加標籤1並轉到標籤2它工作正常,但是當我從標籤2切換到標籤1時,它具有與標籤2相同的狀態 – asdfdefsad

相關問題