6.4 KiB
6.4 KiB
id, title, challengeType, isRequired, videoUrl, localeTitle
| id | title | challengeType | isRequired | videoUrl | localeTitle |
|---|---|---|---|---|---|
| 5a24c314108439a4d4036169 | Pass Props to a Stateless Functional Component | 6 | false | Передача реквизитов функциональному компоненту без состояния |
Description
App который отображает дочерний компонент Welcome который является функциональным компонентом без состояния. Вы можете пройти Welcome в user собственность, написав: <App>Вы используете пользовательские атрибуты HTML, которые React предоставляет поддержку для передачи
<Welcome user = 'Mark' />
</ Приложение>
user свойства в компонент Welcome . Поскольку Welcome является функциональным компонентом без состояния, он имеет доступ к этому значению следующим образом: const Добро пожаловать = (реквизит) => <h1> Здравствуйте, {props.user}! </ h1>Стандартно вызывать это значение
props и при работе с функциональными компонентами без состояния, вы в основном рассматриваете его как аргумент функции, которая возвращает JSX. Вы можете получить доступ к значению аргумента в теле функции. С компонентами класса вы увидите, что это немного отличается. Instructions
CurrentDate компоненты Calendar и CurrentDate . При рендеринге CurrentDate из компонента Calendar передайте свойство date назначенное текущей дате из объекта Date JavaScript. Затем получите доступ к этой prop в компоненте CurrentDate , показывая ее значение в тегах 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"; })(), "The <code>Calendar</code> component should return a single <code>div</code> element.");'
- text: Второй дочерний компонент компонента <code>Calendar</code> должен быть компонентом <code>CurrentDate</code> .
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); return mockedComponent.children().childAt(1).name() === "CurrentDate"; })(), "The second child of the <code>Calendar</code> component should be the <code>CurrentDate</code> component.");'
- text: 'Компонент <code>CurrentDate</code> должен иметь опору, названную <code>date</code> .'
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); return mockedComponent.children().childAt(1).props().date })(), "The <code>CurrentDate</code> component should have a prop called <code>date</code>.");'
- text: <code>CurrentDate</code> <code>date</code> <code>CurrentDate</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 ); })(), "The <code>date</code> prop of the <code>CurrentDate</code> should contain a string of text.");'
- text: Компонент <code>CurrentDate</code> должен отображать значение с <code>date</code> prop в теге <code>p</code> .
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(Calendar)); return mockedComponent.find("p").html().includes(Date().substr(3)); })(), "The <code>CurrentDate</code> component should render the value from the <code>date</code> prop in the <code>p</code> tag.");'
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
console.info('after the test');
Solution
// solution required