modul:ffit:react:learningunits:lu01:defining-a-component

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
modul:ffit:react:learningunits:lu01:defining-a-component [2024/12/05 06:59] kdemircimodul:ffit:react:learningunits:lu01:defining-a-component [2024/12/05 07:08] (aktuell) kdemirci
Zeile 13: Zeile 13:
 ===== Return markup ===== ===== Return markup =====
  
-Every component has to return a value. We create a <color #00008B>return( )</color> with JSX Markup. This is a special Markup Language made for React to write HTML alike in JS.+Each component must return a value. We create a <color #00008B>return()</color> using JSX markup. This is a special markup language made for React that allows HTML to be written in JS as well.
  
-<code>+<code javascript>
 function Product() { function Product() {
   return (   return (
     <div>     <div>
-      <img src="https://i.imgur.com/MK3eW3As.jpg" alt="Katherine Johnson" />+      <img src="https://placehold.it/800" alt="Product Image" /> 
 +      <h2>Product Name</h2> 
 +      <p>Product Description</p> 
 +      <p>$20</p> 
 +    </div> 
 +  ); 
 +
 +</code> 
 + 
 +===== Export the component ===== 
 + 
 +Now we have a componentBut if we want to use it in other files, we need to export the function. To export a component, you need to add the keywords "export default" before the function declaration. This will mark the function to be exported. Now you can include the file in another file to use this component. 
 + 
 +Product.js 
 +<code javascript> 
 +export default function Product() { 
 +  return ( 
 +    <div> 
 +      <img src="https://placehold.it/800" alt="Product Image/> 
 +    </div> 
 +  ); 
 +
 +</code> 
 + 
 +ProductsList.js 
 +<code javascript> 
 +import Product from './Product.js'; 
 + 
 +export default function ProductsList() { 
 +  return ( 
 +    <div> 
 +      <h1>Our Products</h1> 
 +      <Product /> 
 +      <Product /> 
 +      <Product /> 
 +      <Product />
     </div>     </div>
   );   );
 } }
 </code> </code>
  • modul/ffit/react/learningunits/lu01/defining-a-component.1733378352.txt.gz
  • Zuletzt geändert: 2024/12/05 06:59
  • von kdemirci