freeCodeCamp/curriculum/challenges/italian/03-front-end-development-li.../react/use-default-props.md

2.1 KiB

id title challengeType forumTopicId dashedName
5a24c314108439a4d403616b Usare le proprietà predefinite 6 301418 use-default-props

--description--

React ha anche un'opzione per impostare props predefinite. È possibile assegnare props predefinite a un componente come una proprietà del componente stesso, in modo che React le assegni quando necessario. Questo ti permette di specificare quale valore dovrebbe avere una prop se nessun valore è esplicitamente fornito. Ad esempio, se dichiari MyComponent.defaultProps = { location: 'San Francisco' }, hai definito una prop location impostata sulla stringa San Francisco, a meno che non specifichi diversamente. React assegna le prop di default se non sono definite, ma se passi null come valore per una prop, essa rimarrà pari a null.

--instructions--

L'editor di codice mostra un componente ShoppingCart. Definisci gli elementi predefiniti su questo componente in modo che specifichino una prop items con un valore di 0.

--hints--

Il componente ShoppingCart dovrebbe essere presentato.

assert(
  (function () {
    const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart));
    return mockedComponent.find('ShoppingCart').length === 1;
  })()
);

Il componente ShoppingCart dovrebbe avere una prop predefinita di { items: 0 }.

assert(
  (function () {
    const mockedComponent = Enzyme.mount(React.createElement(ShoppingCart));
    mockedComponent.setProps({ items: undefined });
    return mockedComponent.find('ShoppingCart').props().items === 0;
  })()
);

--seed--

--after-user-code--

ReactDOM.render(<ShoppingCart />, document.getElementById('root'))

--seed-contents--

const ShoppingCart = (props) => {
  return (
    <div>
      <h1>Shopping Cart Component</h1>
    </div>
  )
};
// Change code below this line

--solutions--

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

// Change code below this line
ShoppingCart.defaultProps = {
  items: 0
}