微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将数据推送到数组ReactJs

如何解决如何将数据推送到数组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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。