2.2 KiB
2.2 KiB
id, challengeType, forumTopicId, title
id | challengeType | forumTopicId | title |
---|---|---|---|
5a24c314108439a4d403616b | 6 | 301418 | 使用默认的 Props |
Description
MyComponent.defaultProps = { location: 'San Francisco' }
,即定义一个 location 属性,并且其值在没有另行制定的情况下被设置为字符串San Francisco
。如果 props 未定义,则 React 会分配默认 props,但如果你将null
作为 prop 的值,它将保持null
。
Instructions
ShoppingCart
组件。在这个组件上定义默认 props,它指定一个items
prop,其值为0
。
Tests
tests:
- text: 应该渲染<code>ShoppingCart</code>组件。
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); return mockedComponent.find('ShoppingCart').length === 1; })());
- text: '<code>ShoppingCart</code>组件应该有一个<code>{ items: 0 }</code>的默认 prop。'
testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); mockedComponent.setProps({items: undefined}); return mockedComponent.find(''ShoppingCart'').props().items === 0; })());'
Challenge Seed
const ShoppingCart = (props) => {
return (
<div>
<h1>Shopping Cart Component</h1>
</div>
)
};
// change code below this line
After Test
ReactDOM.render(<ShoppingCart />, document.getElementById('root'))
Solution
const ShoppingCart = (props) => {
return (
<div>
<h1>Shopping Cart Component</h1>
</div>
)
};
// change code below this line
ShoppingCart.defaultProps = {
items: 0
}