我正在尝试渲染保持状态的对象。 this.state
的内容是:
"statement": {
"creator": "8ff243b2-f21e-43f3-9090-4aa679fbeb1a",
"id": "bf4c965e-bd59-48c8-b31a-8f67529e5fde",
"impactors": [
"978388e8-2987-4c89-82b6-4da619d82935",
"1d75e2a7-bf2a-4f55-ba68-373752b24f98"
],
"score": {
"impactors": 2,
"net": 3,
"percentage": 0.75,
"total_votes": 4
},
"statement": "The Earth is round."
}
在我的React JS应用程序中,我能够渲染一些子对象(语句),但不是全部。这是我的代码:
renderStatement() {
return (
<tbody>
<tr>
<td>Statement: </td>
<td>{this.state.statement.score}</td>
</tr>
</tbody>
)
}
如预期的那样,以上代码返回错误:Error: Objects are not valid as a React child (found: object with keys {impactors, net, percentage, total_votes}). If you meant to render a collection of children, use an array instead.
我真正想要的是percentage
,它是score
下的一个节点。当我尝试向下钻取并仅渲染percentage
时,就像这样:
renderStatement() {
return (
<tbody>
<tr>
<td>Statement: </td>
<td>{this.state.statement.score.percentage}</td>
</tr>
</tbody>
)
}
我收到以下错误:TypeError: Cannot read property 'percentage' of undefined.
其他任何对象('creator', 'id', 'impactors', 'statement'
)都可以正常工作。
答案 0 :(得分:0)
这是我解决的方法:
renderStatement() {
const score = this.state.statement.score || {}
return (
<tbody>
<tr>
<td>Statement: </td>
<td>{score.percentage}</td>
</tr>
</tbody>
)
}
答案 1 :(得分:-1)
您可以执行以下操作:
TEST(A_Test, Test_function_from_a_to_test)
{
B b;
A a(&b);
EXPECT_CALL(b, function_from_b)
.Times(1);
a.function_from_a_to_test(0);
}