在React组件中使用concat设置状态

时间:2018-12-02 19:41:38

标签: javascript reactjs

我正在使用// imports in my app.js file import React from 'react'; import { library } from '@fortawesome/fontawesome-svg-core'; import { faCoffee } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faFacebookF } from '@fortawesome/free-brands-svg-icons'; library.add(faCoffee, faFacebookF) //this works <FontAwesomeIcon icon="coffee" /> //this does not <FontAwesomeIcon icon={"['fab', 'facebook-f']"} />函数通过Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile] @="Java Program File" [HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile\shell] [HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile\shell\open] [HKEY_CURRENT_USER\SOFTWARE\Classes\jarfile\shell\open\command] @="C:\\Folder\\javaw.exe -jar \"%1\"" [HKEY_CURRENT_USER\SOFTWARE\Classes\.jar] @="jarfile" "Content Type"="text/plain" "PerceivedType"="text" [HKEY_CURRENT_USER\SOFTWARE\Classes\.jar\PersistentHandler] @="{5e941d80-bf96-11cd-b579-08002b30bfeb}" [HKEY_CURRENT_USER\SOFTWARE\Classes\.jar\ShellNew] "ItemName"=hex(2):6a,00,61,00,72,00,66,00,69,00,6c,00,65,00,00,00 "NullFile"="" 向数组添加对象,但是此后我的状态仍然保持初始状态。我错过了一些找不到的东西。这是我的组件。

this.setState

concat函数中,我向const tiles = [ {time: [0,0], text: 'first tile', color: 'red'}, {time: [2,0], text: 'second tile', color: 'green'}, {time: [1,3], text: 'third tile', color: 'lightblue'}, {time: [4,6], text: 'forth tile', color: 'blue'}, {time: [0, 23], text: 'fifth tile', color: 'grey'} ] class CalendarGrid extends Component { constructor(){ super(); this.state = { tiles: tiles, showModal: false } this.addEvent = this.addEvent.bind(this); this.handleOpenModal = this.handleOpenModal.bind(this); this.handleCloseModal = this.handleCloseModal.bind(this); } addEvent(event){ console.log(event); let newTiles = this.state.tiles.concat(event); this.setState({ tiles: newTiles }); console.log(newTiles); console.log(this.state.tiles); this.handleCloseModal(); } render(){ const gridTiles = []; for (var i = 0; i < 168; i++) { let x = i % 7; let y = Math.floor(i / 7); // console.log(this.state.tiles); let event = this.state.tiles.find( tile => tile.time[0] === x && tile.time[1] === y); if (event) { gridTiles.push(this.renderTile(i, event)); } else { gridTiles.push(this.renderTile(i, null)); } } return( <div className="calendar-grid"> <div className="header"> <div id="weekday"> {this.getWeekDays()} </div> </div> <div className="container"> <div id="hour-numbers"> {this.getHours()} </div> <div className="day"> {gridTiles} </div> </div> <AddButton showModal={this.handleOpenModal} /> <AddEventModal addEvent={this.addEvent} showModal={this.state.showModal}/> </div> ) } } 数组添加了一个新对象,但是addEvent()没有修改数组。

0 个答案:

没有答案