Append New React Component onclick

barskyn

I am relatively new to React, but familiar with JavaScript. I want to make a really simple app and in JavaScript whenever I want to append a new HTML element I do something like this: document.getElementById("root").innerHTML += "<h1>Title</h1>";. In React I want to append a new MyComponent component to my page whenver my button is clicked. How can I do this in a similar way to .innerHTML +=. Below is what I have so far to give an idea, but it does not work.

index.js:

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

App.js:

function my_func() {
  var prop1 = prompt("Input here ");
  var prop2 = "new_id";
  document.getElementById("app-root").innerHTML += <MyComponent p1={ prop1 } p2={ prop2 }/>;
}

function App() {
  return (
      <div id="app-root">
      <Button color="primary" onClick={ my_func }>Add</Button>{' '}
      </div>
  );
}

export default App;
development-ninja

You should implement React State here. List of components that you will add is saved to this.state. Here is my suggestion.

This is App.js

import React, { Component } from 'react';
class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      clicked: false,
      mycomponentlist:[]
    };
    this.my_func = this.my_func.bind(this);
  }
  my_func(){
     let {mycomponentlist} = this.state;
     var prop1 = prompt("Input here ");
     var prop2 = "new_id";
     mycomponentlist.push({prop1,prop2});
     this.setState({mycomponentlist,clicked:true});
  } 
  render() {
    const {clicked,mycomponentlist} = this.state;
    return (
      <div id="app-root">
         <button  onClick={this.my_func }>Add</button>
         {clicked&& mycomponentlist.map(mycomponent=> <MyComponent p1={ mycomponent.prop1 } p2={ mycomponent.prop2 }/>)}       
      </div>
    );
  }
}

export default App;

This is MyComponent.js

import React, { Component } from 'react';
class MyComponent extends Component {
    render() {
        const { p1,p2} = this.props;
        return (
            <div >
                //you can use p1,p2 here...
                <p>{p1}</p>
                <p>{p2}</p>
            </div>
        )
    }
}
export default MyComponent;

I am sure this will work. When button first clicked then clicked status becomes true so array of components are shown every render.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

onClick doesn't render new react component.

From Dev

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

From Dev

React — onClick in component instance?

From Dev

React onClick event on component

From Dev

Handling an onClick on a component in React

From Dev

React - return component onClick event

From Dev

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

From Java

How to use onClick event on react Link component?

From Dev

React component how to simplify this onClick function?

From Java

onClick works but onDoubleClick is ignored on React component

From Dev

Meteor React component onClick event not working in IE

From Dev

onClick event isn't firing on React Component

From Dev

Testing onClick event on react component using Jasmine

From Dev

Call React Component Function from onclick in dangerouslySetInnerHTML

From Dev

onClick event isn't firing on React Component

From Dev

Access a parent component's method onClick in React

From Dev

React Component's onClick handler not binding to "this"

From Dev

"Cannot read property 'onClick' of undefined" in React component

From Dev

React--Button onclick event not working in Component

From Dev

React nested component wrong props function onClick

From Dev

New React module inside another react component

From Dev

New React module inside another react component

From Dev

Render new element onClick in react.js

From Dev

Why does a react onClick event append a question mark to my url?

From Dev

How can I append external DOM to React component?

From Dev

React: How to dynamically append child components of differing types into a parent component?

From Dev

React Router - new component not being loaded

From Dev

inject content from one react component to another onClick

From Dev

Why does `this` keyword refer to Window not Component in React onClick() method

Related Related

  1. 1

    onClick doesn't render new react component.

  2. 2

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

  3. 3

    React — onClick in component instance?

  4. 4

    React onClick event on component

  5. 5

    Handling an onClick on a component in React

  6. 6

    React - return component onClick event

  7. 7

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

  8. 8

    How to use onClick event on react Link component?

  9. 9

    React component how to simplify this onClick function?

  10. 10

    onClick works but onDoubleClick is ignored on React component

  11. 11

    Meteor React component onClick event not working in IE

  12. 12

    onClick event isn't firing on React Component

  13. 13

    Testing onClick event on react component using Jasmine

  14. 14

    Call React Component Function from onclick in dangerouslySetInnerHTML

  15. 15

    onClick event isn't firing on React Component

  16. 16

    Access a parent component's method onClick in React

  17. 17

    React Component's onClick handler not binding to "this"

  18. 18

    "Cannot read property 'onClick' of undefined" in React component

  19. 19

    React--Button onclick event not working in Component

  20. 20

    React nested component wrong props function onClick

  21. 21

    New React module inside another react component

  22. 22

    New React module inside another react component

  23. 23

    Render new element onClick in react.js

  24. 24

    Why does a react onClick event append a question mark to my url?

  25. 25

    How can I append external DOM to React component?

  26. 26

    React: How to dynamically append child components of differing types into a parent component?

  27. 27

    React Router - new component not being loaded

  28. 28

    inject content from one react component to another onClick

  29. 29

    Why does `this` keyword refer to Window not Component in React onClick() method

HotTag

Archive