mkdasso2 / flw2-u2l2-23-24-student-exercises-part-1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lesson 2.2 - Components, JSX, and Props

πŸ” Key Concepts

React Components

  • Definition: Functions that return UI elements to be rendered
  • Key Characteristics:
    • Independent and reusable
    • Can return HTML through JSX
    • Start with capital letters
    • Can be used multiple times, each use being an instance

JSX (JavaScript XML)

  • Definition: Syntax extension for JavaScript, allowing UI structuring with a syntax similar to XML/HTML.
  • Key Characteristics:
    • Use className instead of class
    • Components are self-closing (like <Component />)
    • Place JavaScript within curly braces {}
    • Only one root HTML element per component

Props (Properties)

  • Definition: Arguments passed into React components
  • Key Characteristics:
    • Allow dynamic data pass between components
    • Make components reusable
    • Defined as attributes in component tags (e.g., <Component prop="value" />)
    • Accessed within components as props.propName

πŸš€ Quick Code Reference

Basic React Component with Props

import React from "react";

function GreetByName(props) {
  return <div>Hello {props.name}!</div>;
}

function App() {
  return (
    <div className="App">
      <GreetByName name="Jared" />
    </div>
  );
}

export default App;

Passing and Accessing Multiple Props

function Greet(props) {
  return <div>Hello {props.firstName} {props.lastName}!</div>;
}

function App() {
  return (
    <div>
      <Greet firstName="Jane" lastName="Doe" />
    </div>
  );
}

πŸ“š Additional Resources

About


Languages

Language:JavaScript 74.1%Language:HTML 18.0%Language:Nix 5.0%Language:CSS 2.9%