My dad gave me a homework to make him an app that can calculate his bills for the house, so i have a title and 2 inputs in each row. Can i make an array of objects in the state and call them from an another component and just map them out and get the values from them?
I am working in react. I have tried to make an array of inputs that i can get values from them with just one function.
state = {
ponovo: [
{
id: 1,
title: 'Izaberi ponovo 1: ',
iznos: ''
},
{
id: 2,
title: 'Izaberi ponovo 2: ',
iznos: ''
},
{
id: 3,
title: 'Izaberi ponovo 3: ',
iznos: ''
}
]}
handleChange = (id, iznos, event, title) => {
this.setState({
ponovo: this.state.ponovo.map(ponovo => {
if (ponovo.id === id) {
ponovo.iznos = iznos
return (
{ iznos: event.target.value }
);
} return ponovo;
})
});
console.log(id, iznos, title);}
<Ponovo ponovo={this.state.ponovo} handleChange={this.handleChange} />
//Ponovo.js
{this.props.ponovo.map((ponovo) => (
<Ponova key={ponovo.id} ponovo={ponovo} handleChange={this.props.handleChange} />))}
//Ponova.js
{title}
<input type='Number' value={iznos} onChange ={this.props.handleChange.bind(this, title, iznos, id, completed)} />
{iznos}
I cannot enter the values from these inputs, or i can not get values from them. I think that problem is in my handleChange function, i am matching them to their id, but for some reason i can't use event.target.value on them. Any suggestions, tips, help? Thanks! :)