0
我想在父类Component
类中创建一个动态Element
。它在函数声明时给出Unexpected token
。但是在return(..here..)
作品里写同样的功能。我错过了什么?
这是我的代码:为什么declearing函数(Element)外渲染不起作用?
import React, { Component } from 'react';
import '../App.css';
var axios = require('axios');
class DisplayRevenue extends Component {
constructor(props){
super(props);
this.state = { data:[] }
}
componentWillMount() {
this.loadRevenue(this.props.url, this.props.token);
}
setData(data){
this.setState(data:data);
console.log(this.state.data); //this gives output as json object
}
loadRevenue(url,token){
axios({
method:'get',
url:url,
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((response) => {
// console.log(response.data);
this.setData(response.data);
})
.catch(function (error) {
console.log("Error in loading Revenue "+error);
});
}
var ListData = this.state.data.map((invoice) => {return <div>{invoice.customerNumber}</div>})
//above function gives error
render() {
//var listData = this.state.data.map((invoice) => (<div>{invoice.customerNumber}</div>)
return (
<div>
<h3>MonthToDate</h3>
{this.state.data.map((invoice) => {return <div>{invoice.customerNumber}</div>})}
</div>
);
}
}
export default DisplayRevenue;
我JSON对象数组如下:
"data": [
{
"customerId": 0,
"customerNumber": "IT8SDS",
"customerType": "RVN",
"invoiceType": "LBR",
"invoiceAmt": "52651.2287",
"invoiceStatus": "BILLED",
"invoiceDate": "2016-12-30T00:00:00.000Z"
},
{
"customerId": 1,
"customerNumber": "DC0WTY",
"customerType": "RVN",
"invoiceType": "RNT",
"invoiceAmt": "198503.1828",
"invoiceStatus": "BILLED",
"invoiceDate": "2016-12-30T00:00:00.000Z"
},
{
"customerId": 2,
"customerNumber": "LK8MD5",
"customerType": "INT",
"invoiceType": "EQT",
"invoiceAmt": "-6833.70721",
"invoiceStatus": "PENDING",
"invoiceDate": "2016-12-30T00:00:00.000Z"
},
{
"customerId": 3,
"customerNumber": "E03PTJ",
"customerType": "PCT",
"invoiceType": "PTS",
"invoiceAmt": "55670.17911",
"invoiceStatus": "BILLED",
"invoiceDate": "2016-12-19T00:00:00.000Z"
},
注:写作{this.state.data.map((invoice) => {return <div>{invoice.customerNumber}</div>})}
内部回报率(..在这里..)在渲染()的作品。
这样做会导致'编译失败'错误。 'displayRevenue.js:'constunListData'的意外的标记'即在常量ListData的声明处 – noobie
我使用了在您的环境中可能不支持的es6箭头函数语法。你可以使用正常的es5语法。我会更新答案。 –
不,我正在使用es6。 – noobie