class
语法。在以下示例中, Kitten
扩展了React.Component
: class Kitten扩展了React.Component {这将创建一个扩展
构造函数(道具){
超级(道具);
}
render(){
回来(
<H1>,您好</ H1>
);
}
}
React.Component
类的ES6类Kitten
。因此, Kitten
类现在可以访问许多有用的React功能,例如本地状态和生命周期钩子。如果您还不熟悉这些术语,请不要担心,在以后的挑战中将更详细地介绍它们。另请注意, Kitten
类在其中定义了一个调用super()
的constructor
函数。它使用super()
来调用父类的构造函数,在本例中为React.Component
。构造函数是在使用class
关键字创建的对象初始化期间使用的特殊方法。最好用super
调用组件的constructor
,并将props
传递给它们。这可确保组件正确初始化。现在,请知道包含此代码是标准的。很快你会看到构造函数和props
其他用途。 MyComponent
是使用类语法在代码编辑器中定义的。完成编写render
方法,以便返回包含带有文本Hello React!
的h1
的div
元素Hello React!
。 div
元素。
testString: 'assert(Enzyme.shallow(React.createElement(MyComponent)).type() === "div", "The React component should return a div
element.");'
- text: 返回的div
应该在其中呈现一个h1
头。
testString: 'assert(/div
should render an h1
header within it.");'
- text: h1
标头应该包含字符串Hello React!
。
testString: 'assert(Enzyme.shallow(React.createElement(MyComponent)).html() === "h1
header should contain the string Hello React!
.");'
```
## Challenge Seed