1 of 17

Conditional Rendering &

Rendering Lists &

Handling Events

React Week 04

2 of 17

Schedule

01

02

03

04

05

07

08

06

Recap

Conditional Rendering

Exercise

Break!

Rendering Lists

Handling events

Let’s code together !

Let’s code together !

15 min

10 min

15 min

15 min

15 min

15 min

15 min

15 min

3 of 17

Recap

Previously on ReDi React

Components and component types�State vs Props�Special Props ( key ,children ,ref )

4 of 17

Conditional Rendering

conditional rendering in React works the same way conditions work in JavaScript. Use JavaScript operators like if or the conditional operator to create elements representing the current state, and let React update the UI to match them

Let’s code the example together:�https://codesandbox.io/s/conditional-rendering-jxe4g

5 of 17

Exercise 01

Let’s create a real world example !

A navbar with login button VS logout button and a user greeting

https://codesandbox.io/s/conditional-rendering-exercises-wsp25

6 of 17

Questions?

7 of 17

Rendering Lists

  • When you need to render multiple similar components
  • When you don’t know how many items you need to render
  • In React we use map() to loop over an array and render a list

8 of 17

Key prop

  • React will warn you that a key should be provided for list items
  • A “key” is a special string attribute you need to include when creating lists of elements
  • Keys help React identify which items have changed, are added, or are removed
  • When you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort ( which is the default behavior of React )
  • A good rule of thumb is that elements inside the map() call need keys.

9 of 17

Break!

10 of 17

Examples

Let’s render the list and solve a bug together with Key

https://codesandbox.io/s/list-keys-81sei

Why do we need a key ?

https://codesandbox.io/s/rendering-lists-focus-demo-1gl51

11 of 17

Event Handling

12 of 17

Event Handling

  • React events are named using camelCase, rather than lowercase
  • With JSX you pass a function as the event handler, rather than a string.
  • Event object , preventDefault.. etc

13 of 17

Example

Let’s dive into event handlers together !

  • We will Add a focus event handlers to the inputs
  • Display the value in focus on screen
  • Clear the value on blur
  • We can add event listener to any DOM node (add click handler to App div)�

https://codesandbox.io/s/list-keys-81sei

14 of 17

Questions?

15 of 17

Exercise 02

16 of 17

Homework

17 of 17

Resources

TBA

DOCS & WALKTHROUGHS

FURTHER READING / STRETCH