enter code here
我想从profilesProject
内的'for'循环(qualityPname
)中检索一个String变量(componentDidMount()
),但它甚至没有进入使用'for'循环运行。
循环工作正常,连接文本因为我在导出函数中测试。
有些线索我做错了什么?提前谢谢。
import React from 'react';
import .... from ...
export default class MPApp extends React.PureComponent {
state = {
name: '',
profilesProject: '',
};
componentDidMount() {
findQG(this.props.project).then(
(valuesReturnedByAPI) => {
this.setState({
name: valuesReturnedByAPI
});
}
);
qualityPname(project) {
const numberQP = project.qp.length;
var profilesProject = " ";
if (numberQP > 0) {
for (var k = 0; k < numberQP; k++) {
profilesProject = project.qp[k].n + " (" + project.qp[k].l + ") ";
console.log(project.qp[k].n);
console.log(project.qp[k].l);
}
console.log(profilesProject);
return profilesProject; //String I want to retun
}
return ("N/A"); //You should return something in that case also.
}
}
render() {
return (
<div className="page page-limited">
<table >
<tbody>
<tr>
<td>QP: {this.state.profilesProject} </td> //here I want to bring the string
</tr>
<tr>
<td>QG: {this.state.name} </td>
</tr>
</tbody>
</table>
</div>
);
}
}
ANSWER1之后的DEV ENV中的错误 - Git bash
Failed to Compile
Error in ./src/main/js/components/MeasuresProjectApp.js
Syntax error: D:/Userfiles/Expression/plugin-example/src/main/js/components/MeasuresProjectApp.js: Unexpected token, expected ; (53:21)
51 |
52 |
> 53 | qualityPname(project){
| ^
54 |
55 | const numberQP = project.qp.length;
56 | console.log(numberOfQualityP);
@ ./src/main/js/app-measures_project.js
答案 0 :(得分:1)
您不希望从函数返回值,您想要使用该值设置状态。试试这个:
componentDidMount() {
findQG(this.props.project).then(
(valuesReturnedByAPI) => {
this.setState({
name: valuesReturnedByAPI
});
}
);
qualityPname(project) {
const numberQP = project.qp.length;
var profilesProject = " ";
if (numberQP > 0) {
for (var k = 0; k < numberQP; k++) {
profilesProject = project.qp[k].n + " (" + project.qp[k].l + ") ";
console.log(project.qp[k].n);
console.log(project.qp[k].l);
}
console.log(profilesProject);
this.setState({ profilesProject })
}
this.setState({ profilesProject: 'N/A' })
}
}