CS220 W6 Spring24
CS220 W6 Spring24
React State
TBS - CS220 1
React Props
TBS - CS220 2
React Props
The list variable as a global variable in the current application.
Used directly from the global scope in the App component, and
later in the List component.
This could work if you only had one global variable, but it isn’t
maintainable with multiple variables across multiple components.
Props in React: pass variables as information from one component
to another component
TBS - CS220 3
React Props
TBS - CS220 5
React Props
TBS - CS220 7
React Props
TBS - CS220 8
React Props
TBS - CS220 10
React Props
• Another use case for React props is
the List component and its
potential child component.
• Couldn’t extract an Item
component from the List
component
• How to pass each item to the
extracted Item component?
• Perform the component extraction
and pass each item along to the
List component’s new child
component.
TBS - CS220 11
React Props
TBS - CS220 13
React State
TBS - CS220 15
React State: Managing Mutable Data
TBS - CS220 16
Mutating React State
TBS - CS220 17
React State
TBS - CS220 18
React State
• How to change information
over time?
• How to notify React to re-
render its component(s)
again?
• A naive (but wrong) approach
would be the following
• In the browser : the output
does not appear below the
HTML input field after typing
into it
TBS - CS220 19
React State
TBS - CS220 23
React State
When the user types into the input field, the input field’s change event is captured
by the event handler.
The handler’s logic uses the event’s value and the state updater function to set
the updated state.
After the updated state is set in a component, the component renders again (the
component function runs again).
The updated state becomes the current state and is displayed in the component’s
JSX.
TBS - CS220 24
React State
TBS - CS220 25
React State (Exercice)
• Put a console.log() into each of your components.
• For example
• The App component: console.log('App renders’)
• The List component: console.log('List renders’)
• ...
• Now check your browser:
• For the first rendering, all loggings should appear
• Once you type into the HTML input field, only the Search component’s
logging should appear.
• React only re-renders this component (and all of its child components),
because its state changed.
TBS - CS220 26
Rendering and Re-rendering in React
TBS - CS220 30
React State
TBS - CS220 33
Callback Handlers in JSX
TBS - CS220 34
Props / State
props are passed down as information from parent to child
components
The Search component does not share its state with other
components: it’s only used and updated by the Search component
TBS - CS220 35
There is no way to
pass information
up the component
tree
props are naturally
only passed
downwards.
TBS - CS220 36
callback handler
A callback function
TBS - CS220 39
callback handler concept
When an (event) handler is passed as props from a parent
component to its child component, it becomes a callback handler.
TBS - CS220 40
Lifting State in React
TBS - CS220 41
Lifting State in React
TBS - CS220 42
Lifting State in React
TBS - CS220 43
lift state up from
Search to App
component to
share the state with
more components
TBS - CS220 44
Lifting State in React
The Search component doesn’t manage the state anymore
If a component needs to use the state (e.g. displaying it), pass it down as props.
TBS - CS220 46
Lifting State in React
• Managing the search feature’s state in the App component
• we can filter the stories with the stateful searchTerm before
passing them as list to the List component:
TBS - CS220 47
TBS - CS220 48
Lifting State in React
• The filter function checks whether the searchTerm is present in our
story item’s title, but it’s still too opinionated about the letter case.
• To fix this problem, we have to lower case the story’s title and the
searchTerm to make them equal
TBS - CS220 49