- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Arkency 的 ReactJS Koans 来学习 React。我陷入了练习 05-Challenge-GroceryList-part-1.jsx。当我在服务器上运行我的代码时,我的代码会正确显示该列表,但是当我运行测试时,我得到“1)应该有一个无序的杂货列表... AssertionError:GroceryItem 应该仅渲染 < li > 标记内的文本。该文本应仅包含杂货商品名称。”有任何想法吗?如果没有他们的评论,我的代码如下:
var React = require("react");
class GroceryList extends React.Component {
constructor(props) {
super(props);
this.state = {
groceries: [ { name: "Apples" } ]
};
}
render() {
for(var index = 0; index < this.state.groceries.length; index++) {
groceriesComponents.push(
<GroceryListItem
grocery={this.state.groceries[index]}
/>
);
}
return (
<ul>
{groceriesComponents}
</ul>
);
}
}
class GroceryListItem extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<li key={this.props}>
{this.props}
</li>
);
}
}
最佳答案
class GroceryListItem extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<li key={this.props}>
{this.props.grocery.name}
</li>
);
}
}
尝试一下。请注意,您使用的 this.props
不会打印杂货店的名称...您必须引用 prop
。就像上面一样。
这段代码实际上是进入杂货 Prop 并获取名称值{this.props.grocery.name}
。
尝试将名称添加到杂货 Prop 中:
groceriesComponents.push(
<GroceryListItem
grocery={this.state.groceries[index].name}
/>
);
然后在您的组件中,您将执行 {this.props.grocery}
对我来说,这听起来像是验证您的代码的程序希望它看起来与那里完全相同。
关于javascript - ReactJS Koans 杂货 list 第 1 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35640504/
我正在尝试创建一个杂货 list 程序。现在我只是在做一些基本的功能。将商品添加到我的购物 list 中、从购物 list 中删除商品、查看购物 list 以及标记我是否已拿起该商品。我困惑于如何让“
我有一个 Angular 应用程序,我正在尝试使其正常工作。显然,只需再一行代码即可使其工作,但我不知道它是什么。我仍在学习 Angular,而且我不太擅长 Javascript。我这样做是为了将输入
昨天我发布了一个有关杂货 list 的问题,并得到了很多帮助。我遇到了另一个问题。这是我的代码: item = {} while True: x = input('enter item: ')
我正在尝试使用 Arkency 的 ReactJS Koans 来学习 React。我陷入了练习 05-Challenge-GroceryList-part-1.jsx。当我在服务器上运行我的代码时,
我是一名优秀的程序员,十分优秀!