Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js: How to append a component on click?

I'm new to React and I'm puzzled on something kind of basic.

I need to append a component to the DOM after the DOM is rendered, on a click event.

My initial attempt is as follows, and it doesn't work. But it's the best thing I've thought to try. (Apologies in advance for mixing jQuery with React.)

    ParentComponent = class ParentComponent extends React.Component {       constructor () {         this.addChild = this.addChild.bind(this);       }        addChild (event) {         event.preventDefault();         $("#children-pane").append(<ChildComponent/>);       }        render () {         return (           <div className="card calculator">             <p><a href="#" onClick={this.addChild}>Add Another Child Component</a></p>             <div id="children-pane">               <ChildComponent/>             </div>           </div>         );       }     }; 

Hopefully it's clear what I need to do, and I hope you can help me attain an appropriate solution.

like image 388
jayqui Avatar asked Mar 10 '16 01:03

jayqui


People also ask

How do I add a component to a click in React?

Adding Components on Button Click js'; function App() { const [components, setComponents] = useState(["Sample Component"]); function addComponent() { setComponents([...components, "Sample Component"]) } return ( <div> <Button onClick={addComponent} text="Call Component"/> {components.

Can we render component on button click React?

To show or hide another component on click in React: Set the onClick prop on an element. When the element is clicked, toggle a state variable that tracks if the component is shown. Conditionally render the component based on the state variable.

How do I change the onClick component in React?

We have to set initial state value inside constructor function and set click event handler of the element upon which click, results in changing state. Then pass the function to the click handler and change the state of the component inside the function using setState.


2 Answers

Don't use jQuery to manipulate the DOM when you're using React. React components should render a representation of what they should look like given a certain state; what DOM that translates to is taken care of by React itself.

What you want to do is store the "state which determines what gets rendered" higher up the chain, and pass it down. If you are rendering n children, that state should be "owned" by whatever contains your component. eg:

class AppComponent extends React.Component {   state = {     numChildren: 0   }    render () {     const children = [];      for (var i = 0; i < this.state.numChildren; i += 1) {       children.push(<ChildComponent key={i} number={i} />);     };      return (       <ParentComponent addChild={this.onAddChild}>         {children}       </ParentComponent>     );   }    onAddChild = () => {     this.setState({       numChildren: this.state.numChildren + 1     });   } }  const ParentComponent = props => (   <div className="card calculator">     <p><a href="#" onClick={props.addChild}>Add Another Child Component</a></p>     <div id="children-pane">       {props.children}     </div>   </div> );  const ChildComponent = props => <div>{"I am child " + props.number}</div>; 
like image 110
Alex McMillan Avatar answered Oct 10 '22 01:10

Alex McMillan


As @Alex McMillan mentioned, use state to dictate what should be rendered in the dom.

In the example below I have an input field and I want to add a second one when the user clicks the button, the onClick event handler calls handleAddSecondInput( ) which changes inputLinkClicked to true. I am using a ternary operator to check for the truthy state, which renders the second input field

class HealthConditions extends React.Component {   constructor(props) {     super(props);       this.state = {       inputLinkClicked: false     }   }    handleAddSecondInput() {     this.setState({       inputLinkClicked: true     })   }     render() {     return(       <main id="wrapper" className="" data-reset-cookie-tab>         <div id="content" role="main">           <div className="inner-block">              <H1Heading title="Tell us about any disabilities, illnesses or ongoing conditions"/>              <InputField label="Name of condition"               InputType="text"               InputId="id-condition"               InputName="condition"             />              {               this.state.inputLinkClicked?                <InputField label=""                 InputType="text"                 InputId="id-condition2"                 InputName="condition2"               />                :                <div></div>             }              <button               type="button"               className="make-button-link"               data-add-button=""               href="#"               onClick={this.handleAddSecondInput}             >               Add a condition             </button>              <FormButton buttonLabel="Next"               handleSubmit={this.handleSubmit}               linkto={                 this.state.illnessOrDisability === 'true' ?                 "/404"                 :                 "/add-your-details"               }             />              <BackLink backLink="/add-your-details" />            </div>          </div>       </main>     );   } } 
like image 25
Hom Bahrani Avatar answered Oct 10 '22 01:10

Hom Bahrani