3

I am trying to add a component on click of button.

Following is the fiddle for that

https://jsfiddle.net/rzv6Lrjh/92/

 render: function() {
   return (
     <div>
       <IndividualTicketInput />
       {this.state.tickets}
       <CreateTicket createTicket={this.onClick} />
     </div>
   );
  }
});

Here I am using individual component IndividualTicketInput , is it possible to do it inside single component Tickets component?

2
  • 1
    this is what you want? jsfiddle.net/mayankshukla5031/07v7qfzo i think you don't need to create a separate component for button and instead of pushing the new element in state maintain the count only how many child components you need to create. Then use loop to create equal no of child component. Commented Jun 15, 2017 at 12:39
  • I tried updating your code for a more updated react but I get error Each child in an array or iterator should have a unique "key" prop codesandbox.io/s/k93221y187 Commented Sep 1, 2018 at 18:54

2 Answers 2

3

You could store an array of tickets in state and generate a new ticket object each time you click the CreateTicket button. Store the new tickets in state and iterate over them, rendering each one to the dom. The component will rerender each time setState is called, updating the dom with your new <Ticket> component.

 state = { tickets: [] }

 render: function() {
   return (
     <div>
       <IndividualTicketInput />
       {this.state.tickets}
       <CreateTicket createTicket={this.onClick} />
       {this.renderTickets()}
     </div>
   );
  }
});

renderTickets() {
   return this.state.tickets.map(ticket => {
      return <Ticket key={ticket.id} ticket={ticket} />;
   });
}

onClick = () => {
   let newTicket = { ... };
   let tickets = this.state.tickets.unshift(newTicket);
   this.setState({tickets});
}
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can.

You can implement a function, which is returning the html elements of ticket UI, inside of Tickets component. But, I think it's not the best practice. Because, you should divide each UI component item as React Component.

https://jsfiddle.net/rwnvt8vs/

ticket: function(ticket = {name: '', quantity: '', price: null}){
    return (
    <ul>
        <li>
          <label>Ticket Name</label>
          <input className="ticket-name" type="text" placeholder="E.g. General Admission" value={ticket.name} />
        </li>
        <li>
          <label>Quantity Available</label>
          <input className="quantity" type="number" placeholder="100" value={ticket.quantity} />
        </li>
          <li>
            <label>Price</label>
            <input className="price" type="number" placeholder="25.00" value={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>
  );
},

onClick: function() {
    var newTicket = this.ticket();
    var tickets = this.state.tickets.push(newTicket);
    this.setState({tickets: tickets});
 },

2 Comments

Rather than forcing an update it's better practice to use this.setState. This is because setState re-renders anyway and also batches the rendering.
@DominicTobias I'm fully agree with you but I've just refactored the required part for the answer.

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.