2.7 KiB
2.7 KiB
id | title | challengeType | isRequired | videoUrl | localeTitle |
---|---|---|---|---|---|
5a24c314108439a4d403616c | Override Default Props | 6 | false | 覆盖默认道具 |
Description
Instructions
ShoppingCart
组件现在呈现子组件Items
。此Items
组件的默认prop quantity
设置为整数0
。通过为quantity
传递值10
来覆盖默认支柱。 注意:请记住,向组件添加prop的语法与添加HTML属性的方式类似。但是,由于quantity
的值是一个整数,因此它不会引用引号,但应该用大括号括起来。例如, {100}
。此语法告诉JSX将大括号内的值直接解释为JavaScript。 Tests
tests:
- text: <code>ShoppingCart</code>应该呈现组件。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); return mockedComponent.find("ShoppingCart").length === 1; })(), "The component <code>ShoppingCart</code> should render.");'
- text: 该组件<code>Items</code>应该呈现。
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); return mockedComponent.find("Items").length === 1; })(), "The component <code>Items</code> should render.");'
- text: '<code>Items</code>组件应具有从<code>ShoppingCart</code>组件传递的<code>{ quantity: 10 }</code>的prop。'
testString: 'getUserInput => assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); return mockedComponent.find("Items").props().quantity == 10 && getUserInput("index").replace(/ /g,"").includes("<Itemsquantity={10}/>"); })(), "The <code>Items</code> component should have a prop of <code>{ quantity: 10 }</code> passed from the <code>ShoppingCart</code> component.");'
Challenge Seed
const Items = (props) => {
return <h1>Current Quantity of Items in Cart: {props.quantity}</h1>
}
Items.defaultProps = {
quantity: 0
}
class ShoppingCart extends React.Component {
constructor(props) {
super(props);
}
render() {
{ /* change code below this line */ }
return <Items />
{ /* change code above this line */ }
}
};
After Test
console.info('after the test');
Solution
// solution required