2018-09-30 23:01:58 +01:00
---
id: 5a24c314108439a4d403616c
title: Override Default Props
challengeType: 6
2019-08-05 09:17:33 -07:00
forumTopicId: 301399
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
The ability to set default props is a useful feature in React. The way to override the default props is to explicitly set the prop values for a component.
< / section >
## Instructions
< section id = 'instructions' >
The < code > ShoppingCart< / code > component now renders a child component < code > Items< / code > . This < code > Items< / code > component has a default prop < code > quantity< / code > set to the integer < code > 0< / code > . Override the default prop by passing in a value of < code > 10< / code > for < code > quantity< / code > .
< strong > Note:< / strong > Remember that the syntax to add a prop to a component looks similar to how you add HTML attributes. However, since the value for < code > quantity< / code > is an integer, it won't go in quotes but it should be wrapped in curly braces. For example, < code > {100}< / code > . This syntax tells JSX to interpret the value within the braces directly as JavaScript.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: The component < code > ShoppingCart</ code > should render.
2019-07-24 05:07:46 -07:00
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); return mockedComponent.find('ShoppingCart').length === 1; })());
2018-10-04 14:37:37 +01:00
- text: The component < code > Items</ code > should render.
2019-07-24 05:07:46 -07:00
testString: assert((function() { const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart)); return mockedComponent.find('Items').length === 1; })());
2018-10-23 16:21:53 +03:00
- text: "The < code > Items</ code > component should have a prop of < code > { quantity: 10 }</ code > passed from the < code > ShoppingCart</ code > component."
2019-07-24 05:07:46 -07:00
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 } / > '); })());"
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'jsx-seed' >
```jsx
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() {
2020-09-15 09:53:25 -07:00
{ /* Change code below this line */ }
2018-09-30 23:01:58 +01:00
return < Items / >
2020-09-15 09:53:25 -07:00
{ /* Change code above this line */ }
2018-09-30 23:01:58 +01:00
}
};
```
< / div >
### After Test
< div id = 'jsx-teardown' >
2020-07-13 18:58:50 +02:00
```jsx
2018-10-20 21:02:47 +03:00
ReactDOM.render(< ShoppingCart / > , document.getElementById('root'))
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
2020-07-13 18:58:50 +02:00
```jsx
2018-09-30 23:01:58 +01:00
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() {
2020-09-15 09:53:25 -07:00
{ /* Change code below this line */ }
2018-09-30 23:01:58 +01:00
return < Items quantity = {10} / >
2020-09-15 09:53:25 -07:00
{ /* Change code above this line */ }
2018-09-30 23:01:58 +01:00
}
};
```
< / section >