如何将数据推送到数组ReactJs

问题描述

是否可以将渲染器中的Jans,Februarys,marchs,Aprils…推到名为Revenues的数组中?

constructor(props) {
    super(props);
    this.state = {
      January:[],February:[],march:[],April:[],May:[],June:[],July:[],August:[],September:[],October:[],November:[],December:[],Type: "bar",Revenues:[],}

render这是我想将有价值的解释器值放入数组的部分。

 addMonthToRevenues = (OctobeRSSs) => {
    this.setState({
      Revenues: [ ...this.state.Revenues,OctobeRSSs]
    }) }

我想将计算得出的值放在渲染下,并将其存储在名为Revenues的数组中。

 render {
               const OctobeRSSs =  OctobeRSSs.reduce(( sum,{price,quantity}) =>  sum + price * quantity,0);
               const Februarys =  February.reduce(( sum,0);
               const marchs =  march.reduce(( sum,0);
               const Aprils =  April.reduce(( sum,0);
               const Mays =  May.reduce(( sum,0);
               const Junes =  June.reduce(( sum,0);
               const Julys =  July.reduce(( sum,0);
               const Augusts =  August.reduce(( sum,0);
 }

数据图表

const data = {   labels: [
    'January','February','march','April','May','June','July','August','September','October','November','December'],datasets: [
    {
      label: 'รายรับ',fill: true,lineTension: 0.5,backgroundColor: 'green',borderColor: 'rgba(75,192,1)',borderCapStyle: 'butt',borderDash: [],borderDashOffset: 0.0,borderJoinStyle: 'miter',pointBorderColor: 'rgba(75,pointBackgroundColor: '#fff',pointBorderWidth: 1,pointHoverRadius: 5,pointHoverBackgroundColor: 'rgba(75,pointHoverBorderColor: 'rgba(220,220,pointHoverBorderWidth: 2,poinTradius: 1,pointHiTradius: 10,data: this.state.Revenues
    },] };

图表

<Bar
    data={data}
    width={60}
    height={30}
               />

解决方法

您可以通过这样的东西得到想要的东西

addMonthToRevenues = month => {
    this.setState(prevState => {
        Revenues: [...prevState.Revenues,month]
    })
}

然后在每个要添加的月份调用它

addMonthToRevenues(Januarys)