Set Default Props

how to set default props and use it

Your code so far


const ShoppingCart = (props) => {
  return (
    <div>
      <h1>Shopping Cart Component</h1>
    </div>
  )

};
// change code below this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/use-default-props

An example is given in the instructions.

MyComponent.defaultProps = { location: 'San Francisco' }

Following this syntax, You can replace MyComponent with the name of the component in this exercise. Give items property value of 0.

but where to put default props in the syntax

default props line should go on a separate line. Below

// change code below this line

1 Like

i have also done it bit still it’s not working

Can you share your code?

const ShoppingCart = (props) => { 
 return (
    <div>
      <h1>Shopping Cart Component</h1>
         </div>  )  };
 ShoppingCart.defaultprops = {  items : 0}

You did not put it below the
// change code below this line

Instead you put it inside the render which is not below that commented line. You deleted the commented line? You declare default props OUTSIDE of the Component.

yes i deleted comment

though it was not deleted , it also didn’t work

My bad, you have it in the correct location, but you have a typo.

defaultprops is not the same as defaultProps

ooh…:face_with_raised_eyebrow:

const ShoppingCart = (props) => {

return (

<div>

<h1>Shopping Cart Component</h1>

</div>

)

};

// change code below this line

ShoppingCart.defaultprops = {

items:0

}

Hey is there any thing wrong, I am trying this but it says

The ShoppingCart component should have a default prop of { items: 0 }.

1 Like

Thank you so much Jenovs.