everytime I click I concatenate components and render them to the screen. That works well. But what if I want to take the data(values) from those components and put them within the same array , CompList (or another array)? To be clear, I would all the values in the different input boxes to correspond to their own element within the array ex: if elementName = "" inside index 1 of the array, then "" is the value at the index. How would I go about doing this?
import React, { Component } from 'react';
import './App.css';
import Comp from './Comp';
import Button from './button';
class App extends Component {
state = {
CompList: [],
elementName: ""
}
AddComp = event => {
this.setState(
{CompList: this.state.CompList.concat(this.state.elementName)}
);
}
render() {
return (
<div>
{this.state.CompList.map(el => <Comp/>)}
<Button AddComp = {this.AddComp}/>
</div>
);
}
}
export default App;
Here is the button to click that appends the component. AddComp is sent to this component.
import React from 'react';
const button = props =>{
return(
<div>
<div onClick = {props.AddComp}>
Clickme
</div>
</div>
);
}
export default button;
And here is the component itself (the component that gets iterated and displayed). I hope Im being clear enough. If needed Ill gladly post additional information.
import React from 'react';
const Comp = props =>{
return(
<div>
<input/>
<div>
100
</div>
</div>
);
}
export default Comp;