2.5 KiB
2.5 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
5a24c314108439a4d403616e | 使用 this.props 访问 Props | 6 | 301375 |
--description--
前几项挑战涵盖了将 props 传递给子组件的基本方法。但是,倘若接收 prop 的子组件不是无状态函数组件,而是一个 ES6 类组件又当如何呢?ES6 类组件访问 props 的方法略有不同。
任何时候,只要引用类组件本身,就要使用this
关键字。要访问类组件中的 props,你需要在在访问它的代码前面添加this
。例如,如果 ES6 类组件有一个名为data
的 prop,你可以在 JSX 中这样写:{this.props.data}
。
--instructions--
在父组件ResetPassword
中渲染ReturnTempPassword
组件的一个实例。在这里,为ReturnTempPassword
提供一个tempPassword
prop,并赋值给 prop 一个长度至少为 8 个字符的字符串。在子组件ReturnTempPassword
中,访问strong
标签中的tempPassword
prop,以确保用户看到临时密码。
--hints--
ResetPassword
组件应该返回单个div
元素。
assert(
(function () {
const mockedComponent = Enzyme.mount(React.createElement(ResetPassword));
return mockedComponent.children().type() === 'div';
})()
);
ResetPassword
的第四个子组件应该是ReturnTempPassword
组件。
assert(
(function () {
const mockedComponent = Enzyme.mount(React.createElement(ResetPassword));
return (
mockedComponent.children().childAt(3).name() === 'ReturnTempPassword'
);
})()
);
ReturnTempPassword
组件应该有一个名为tempPassword
的属性。
assert(
(function () {
const mockedComponent = Enzyme.mount(React.createElement(ResetPassword));
return mockedComponent.find('ReturnTempPassword').props().tempPassword;
})()
);
ReturnTempPassword
组件的tempPassword
prop 值应该是一个字符串,其长度至少为8
。
assert(
(function () {
const mockedComponent = Enzyme.mount(React.createElement(ResetPassword));
const temp = mockedComponent.find('ReturnTempPassword').props()
.tempPassword;
return typeof temp === 'string' && temp.length >= 8;
})()
);
ReturnTempPassword
组件应该显示你作为tempPassword
prop 创建的密码,并且密码显示在strong
标签中。
assert(
(function () {
const mockedComponent = Enzyme.mount(React.createElement(ResetPassword));
return (
mockedComponent.find('strong').text() ===
mockedComponent.find('ReturnTempPassword').props().tempPassword
);
})()
);