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;