Hola tengo un componente en React que tiene un state tipo objeto: items: {} y mi objetivo es llenarlo con propiedades tipo arreglo. Ejemplo si ingreso en un input un valor Gato se cree : items:{ Gato[] } y si luego ingreso otro valor se cree items: {Gato[], Perro[],...etc}. En la variable s me esta llegando el valor del input y el array Lists se esta llenando correctamente, el problema es con el objeto items. Por favor alguien que me ayude
import React, { Component } from 'react';
import Lists from './Lists.js';
import AddList from './AddList.js';
import './App.css';
class App extends Component {
constructor() {
super();
this.state = {
lists: [], // this holds the name of each list
items: {} // this property names of this object are the names of the lists;
their values are arrays of the items in each list
};
}
handleAddList(s) {
// Implement this function!
let nameList = s;
this.setState({ lists: this.state.lists.concat(nameList) });
this.setState({ items: this.state.items.[nameList] = [] });
console.log(this.state.items);
}
handleAddItem(s) {
// Implement this function!
let itemName = s;
}
/**
* Renders the component.
*/
render() {
return (
<div className="App">
<AddList addList={this.handleAddList.bind(this)} />
<div id="listsDiv" className="List">
<Lists lists={this.state.lists} items={this.state.items} addItem=
{this.handleAddItem.bind(this)} />
</div>
</div>
);
}
}
export default App;
this.setState({ items: this.state.items.[nameList] = [] });
, debería serthis.setState({ items: this.state.items[nameList] = [] });
.this.setState({ items: { ...this.sate.items, namelist: [] } })
te funciona.setState
lists
eitems
, por que no simplemente hacesthis.state.items[nameList] = [];
sin elsetState