Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append a React component in another on button click

I have a big component <User> and a small one <Task> and want to give a big component a button and on click on it append the component <Task> to the <User>

User.jsx

<div>
  <Task />
  <button onClick={`Function here to append a new Task component`}>
    New Task
  </button>
</div>

Task.jsx

<div>
  This is New task
</div>
like image 482
Zeyad Etman Avatar asked Jul 18 '18 14:07

Zeyad Etman


People also ask

How do you pass data to another component in react on button click?

First, you'll need to create two components, one parent and one child. Next, you'll import the child component in the parent component and return it. Then you'll create a function and a button to trigger that function. Also, you'll create a state using the useState Hook to manage the data.

How do I add react component on button click?

Adding Components on Button Click It's time to implement the creation of new elements when we click the 'Call Component' button. import { useState } from 'react'; import { Button } from './Button. js'; import { ListComponent } from './ListComponent.

How do you add a component to a click?

addChild = this. addChild. bind(this); } addChild (event) { event. preventDefault(); $("#children-pane").


1 Answers

You should let React render everything, and your job is only to tell React what to render and, in your case, how many times. For that, a counter can be used to track how many dynamically-added should be "injected" inside the <Example> component.

A mindshift is needed here, since in your example you are coming from a place where you think that the click handler itself should modify the DOM, and in React that is an antipattern.

Instead, you should work with state, and that means the click handler should update the state of the host component and that will trigger a re-render (this is how React works) and in the next render cycle, your added component will be rendered as many times as the counter value, because that counter change is what triggered the re-rendering.

In React, the props & state are the way to trigger re-render and any DOM modification should be done by changing the internal component's state, or by sending different props, from the parent component.


In the below example I do not use Classes, but use Hooks instead, because I've stopped using class altogether once hooks were released, because I think it's cleaner:

const {useState, Fragment} = React

// The added element component
const AddedElement = () => <p><input placeholder='text box' /></p>

// The parent component
const App = () => {
  const [count, setCount] = useState(0) // Name it however you wish

  return <Fragment>
    <button onClick={() => setCount(count + 1)}>Click me</button>
    { [...Array(count)].map((_, i) => <AddedElement key={i} />) }
  </Fragment>
}

// Render 
ReactDOM.render(<App />, document.getElementById("react"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.11.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script>
<div id="react"></div>

If you are interested in how to repeatedly render the same component, I've written an answer about this in this question: How can I render repeating React elements?

like image 136
vsync Avatar answered Oct 24 '22 09:10

vsync