Full Stack Development

Component Props

by Tomas Trescak t.trescak@westernsydney.edu.au

Props

/

Basic Props

        type GreetingProps = {
  name: string;
}

const Greeting = (props: GreetingProps) => (
  <div>Hello: {props.name}</div>
)

const App = () => (
  <div>
    <Greeting name="Tomas" />
    <Greeting name="Valeria" />
  </div>
)

render(<App />);
      
        body { 
    padding: 16px; 
    font-size: 40px; 
}
      
        
      

Props

/

Types and React.ReactNode

        type GreetingProps = {
  name: string;
}

const Greeting = (props: GreetingProps) => (
  <div>Hello: {props.name}</div>
)

const App = () => (
  <div>
    <Greeting name="Tomas" />
    <Greeting name="Valeria" />
  </div>
)

render(<App />);
      
        body { 
    padding: 16px; 
    font-size: 40px; 
}
      
        
      

Props

/

Types and React.ReactNode

      type GreetingProps = {
  name: string;
}

const Greeting = (props: GreetingProps) => (
  <div>Hello: {props.name}</div>
)

const App = () => (
  <div>
    <Greeting name="Tomas" />
    <Greeting name="Valeria" />
  </div>
)

render(<App />);
    

Props

/

children

      type GroupProps = {
  name: string;
  children: React.ReactNode;
}

const Group = (props: GreetingProps) => (
  <fieldset>
    <legend>{props.name}</legend>
    {props.children}
  </fieldset>
)

const App = () => (
  <div>
    <Group name="To DO">
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
      </ul>
    </Group>
    
    <Group name="Done">
      <ul>
        <li><s>Item 1</s></li>
        <li><s>Item 2</s></li>
      </ul>
    </Group>
  </div>
)

render(<App />);
    

Props

/

Destructuring

      const [a, b] = [1,2];
const { a, b, ...rest } = { a: 1, b: 2, c: 3 };

type Props = { name: string, age: number }

// no destructuring
const Greeting = (props) => (
  <div>
    Hello { props.name }, age { props.age }
  </div>
)

// destructuring
const Greeting = ({ name, ...rest }) => (
  <div>
    Hello { name }, age { rest.age }
  </div>
)
    

Props

/

Default Values

      type Props = { 
  name: string;
  age?: number;
}

const Greeting = ({ name, age = 44 }) => (
  <div>
    Hello { name }, your age { age }
  </div>
)

const App = () => (
  <>
    <Greeting name="Tomas" />
    <Greeting name="Valeria" age={30} />
  </>
)

render(<App />)
    

Props

/

Boolean props

      type Props = {
  name: string;
  age: number;
  showAge: boolean;
}

const App = () => (
  <>
    <Greeting 
        name="Tomas" 
        age={60} 
        showAge 
    />
    <Greeting 
        name="Valeria" 
        age={30}
        showAge={false}
    />
  </>
)
    

Props

/

Spread props

      const Greeting = (props) => (
  <div>
    <h1>Greeting</h1>
    <InnerGreeting showAge {...props} />
  </div> 
)
    

Props

/

Readonly

      const Greeting1 = (props) => {
  props.name = "Tomas"; // NO!

  return (
    <div>
      {props.name}
    </div> 
  )
}

const Greeting2 = ({ name }) => {
  name = "Tomas"; // NO!

  return (
    <div>
      {name}
    </div> 
  )
}

render(<Greeting1 />)
    

Props

/

Exercise