Rendering new component on-click in a parent component

luke

I have a button component called CreateTicket and a component I want to render on click called IndividualTicketInput. I have created a parent component where I currently have both of these. I need to have a function that renders a new ticket component on-click in the parent component underneath the first IndividualTicketInput and so forth. I know I have to push into the tickets array but I am still learning react and having trouble understanding the concepts. How would you best go about this and if you need clarity on my end please let me know.

button component

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

Ticket Component **(this is the component I want to render on click)

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>
    );
  }
});

Tickets Component

var Tickets = React.createClass({
  getInitialState: function() {
    return {tickets: [] };
  },
  createNewTicket: function() {

    // where the push happens on click to create the new IndividualTicketInput component

    // this is what I am currently trying but it isn't working
    var tickets = this.state.tickets;
     this.state.tickets.push(
      <IndividualTicketInput />
    )
    this.forceUpdate();

  },
  render: function() {
    return (
      <div>
        <IndividualTicketInput />
        // new component will render here
        <div>{this.state.tickets}</div>
        <CreateTicket onClick={this.createNewTicket} />
      </div>
    );
  }
});
luke

Got this working. Attached props to the child button component and then passed through the component using push.

button component

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

Tickets component:

var Tickets = React.createClass({
  getInitialState: function() {
    return {
      tickets: []
    }
  },
  componentDidMount: function() {
    console.log('this mounted')
  },
  onClick: function() {
    this.state.tickets.push(
      <IndividualTicketInput />
    )
    this.forceUpdate();
  },
  render: function() {
    return (
      <div>
        <IndividualTicketInput />
        {this.state.tickets}
        <CreateTicket createTicket={this.onClick} />
      </div>
    );
  }
});

The only problem I run into now is the key situation but I will figure it out. Thanks all that took a look at this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create and return a new component on-click in parent component

From Dev

Problems with rendering a component on click

From Dev

React Parent Component Not Rendering Child Component

From Dev

Rendering ember component outside parent template

From Dev

my props.children are not rendering, but they are in the parent component

From Dev

First click not re-rendering component in react

From Dev

Vue.js: Trigger an @click on child component from parent component in

From Dev

How can I update data from parent component when I click child component on the vue component?

From Dev

Rendering React Component into GoogleMaps InfoBox - Stop click throughs

From Dev

react component not re-rendering on state change from parent component firebase

From Dev

React: component only rendering new values on second onClick of button

From Dev

Simple React component not rendering

From Java

Dynamically Rendering a React component

From Dev

QML Component Screen Rendering

From Dev

React router not rendering component

From Dev

Reactjs: Component not rendering

From Dev

ReactSelect rendering custom component

From Dev

change component rendering order

From Dev

Custom PrimeFaces component not rendering

From Dev

Child component is not rendering props

From Dev

Rendering a component using AJAX

From Dev

Ember not rendering component at first

From Dev

KnockoutJS component; Template is not rendering

From Dev

React router not rendering component

From Dev

Rendering a Component Instance in React

From Dev

React not rendering component

From Dev

Rendering one component into another

From Dev

Basic react component not rendering

From Dev

Rendering Math in React Component

Related Related

HotTag

Archive