4.7 KiB
4.7 KiB
id, challengeType, forumTopicId, title
id | challengeType | forumTopicId | title |
---|---|---|---|
5a24c314108439a4d4036169 | 6 | 301402 | 将 Props 传递给无状态函数组件 |
Description
App
组件,该组件渲染了一个名为Welcome
的子组件,它是一个无状态函数组件。你可以通过以下方式给Welcome
传递一个user
属性:
<App>
<Welcome user='Mark' />
</App>
使用自定义 HTML 属性,React 支持将属性user
传递给组件Welcome
。由于Welcome
是一个无状态函数组件,它可以像这样访问该值:
const Welcome = (props) => <h1>Hello, {props.user}!</h1>
调用props
这个值是常见做法,当处理无状态函数组件时,你基本上可以将其视为返回 JSX 的函数的参数。这样,你就可以在函数体中访问该值。但对于类组件,访问方式会略有不同。
Instructions
Calendar
和CurrentDate
组件。从Calendar
组件渲染CurrentDate
时,从 JavaScript 的Date
对象分配当前日期,并将其作为date
属性传入。然后访问CurrentDate
组件的prop
,并在p
标签中显示其值。请注意,要将prop
的值视为 JavaScript,必须将它们括在花括号中,例如date={Date()}
。
Tests
tests:
- text: <code>Calendar</code>组件应该返回单个<code>div</code>元素。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); return mockedComponent.children().type() === 'div'; })());
- text: <code>Calendar</code>组件的第二个子元素应该是<code>CurrentDate</code>组件。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); return mockedComponent.children().childAt(1).name() === 'CurrentDate'; })());
- text: <code>CurrentDate</code>组件应该有一个名为<code>date</code>的属性。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); return mockedComponent.children().childAt(1).props().date })());
- text: <code>CurrentDate</code>的<code>date</code>属性应该包含一段文本字符串。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); const prop = mockedComponent.children().childAt(1).props().date; return( typeof prop === 'string' && prop.length > 0 ); })());
- text: <code>CurrentDate</code>组件应该把<code>date</code>属性渲染在<code>p</code>标签内。
testString: assert(/<CurrentDatedate={Date\(\)}\/>/.test(code.replace(/\s/g, '')))
- text: <code>CurrentDate</code> 组件应该把 <code>date</code> 属性渲染在 <code>p</code> 标签内。
testString: let date = "dummy date"; assert((function() { const mockedComponent = Enzyme.mount(React.createElement(CurrentDate, {date})); return mockedComponent.find('p').html().includes(date); })());
Challenge Seed
const CurrentDate = (props) => {
return (
<div>
{ /* change code below this line */ }
<p>The current date is: </p>
{ /* change code above this line */ }
</div>
);
};
class Calendar extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h3>What date is it?</h3>
{ /* change code below this line */ }
<CurrentDate />
{ /* change code above this line */ }
</div>
);
}
};
After Test
ReactDOM.render(<Calendar />, document.getElementById('root'))
Solution
const CurrentDate = (props) => {
return (
<div>
{ /* change code below this line */ }
<p>The current date is: {props.date}</p>
{ /* change code above this line */ }
</div>
);
};
class Calendar extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<h3>What date is it?</h3>
{ /* change code below this line */ }
<CurrentDate date={Date()} />
{ /* change code above this line */ }
</div>
);
}
};