index.js
import React, { Component } from 'react'; import {render} from 'react-dom'; import ItemsList from "../src/components/App" const propsValues = { title: "Список смартфонов", items: [ "HTC U Ultra", "iPhone 7", "Google Pixel", "Hawei P9", "Meizu Pro 6", "Asus Zenfone 3" ] }; render(<ItemsList data={propsValues} />, document.getElementById('root'));
app.js
import React, { Component } from 'react'; class Item extends React.Component { render() { return <li>{this.props.name}</li>; } } class ItemsList extends React.Component { constructor(props){ super(props); this.state = { items: this.props.data.items}; } render() { return( <div> <h2>{this.props.data.title}</h2> <ul> { this.state.items.map(function(item){ return <Item key={item} name={item} /> }) } </ul> <button onclick={this.appArr}>ok</button> </div>); } } export default ItemsList;
I am trying to add a new array a through setState, but it does not work
function appArr() { let a = [1]; this.setState( { items: [...a, this.props.data.items] } ) }