π Fetching data
Learning Objectives
Often when you create a React app, you will want to fetch data from an API and display it inside your components. How do we do this in React?
You might think that we could just fetch the data in the component like this, but unfortunately it won’t work. Try it out in the CodeSandbox below.
This is because React is synchronous, while fetch
is asynchronous. If we look in the console, we’ll see that the imgSrc
will always be null when we try to render it. React will try to render before fetch
has had time to get the data from the API.
So how do we fix this? We need to use a React hook called useEffect
.
π€ΉπΌ Synchronising with effects
Learning Objectives
narrative
Effect here means “side effect”. We use the useEffect()
hook to run code that has side effects, like fetching data from an API. We use these sparingly, because they can make our code harder to reason about.
question
π‘
tip
π π³ Fetching data with Effects
Learning Objectives
narrative
π Working with forms
Learning Objectives
Modern web applications often involve interacting with forms such as creating an account, adding a blog post or posting a comment. We need to declare form inputs and get the values entered by users to do something with it (like display them on a page or send them in a POST request). So, how do we do this in React?
A popular pattern for building forms and collect user data is the controlled component pattern. A pattern is a repeated solution to a problem that is useful in multiple similar cases. Let’s have a look at an example:
We’re controlling the value of the input by using the value from the reminder state. This means that we can only change the value by updating the state. It is done using the onChange
attribute and the handleChange
function, called every time the input value changes. This happens when a new character is added or removed.
If we didn’t call setReminder
in the handleChange
function, then the input’s value would never change. It would appear as if you couldn’t type in the input!
Finally, the value we keep in the reminder state is displayed on the screen as today’s reminder.
(We could have also transformed the string before we set it with setReminder
, by calling toUpperCase()
on the string.)
ποΈ Forms with multiple fields
Learning Objectives
Let’s have a look at a more complex example. Here we will build a form for users to create a personal account. Make sure to select CreateAccountForm.js
in the menu to you’re looking at the right part of the code.
We now have three different inputs named username
, email
and password
. There is a corresponding state variable and change handler function for each value.
π§Ό Inlining event handlers
We could make our code a bit shorter if we inlined our event handlers:
π§Ί Centralising event handlers
Sometimes we need to put all of our update logic in one function. Maybe we need to validate the user’s input before we set it in state.
We can use a single handleChange
function, that is reused to keep track of changes for all of the form fields. To be able to tell which <input>
is being updated, we check the event.target.name
field. This corresponds to the name attribute on the <input>
(e.g. <input name="username">
).
Based on this value, we then decide which state to update:
πͺ€ Controlled Components
Learning Objectives
narrative
question
Youtube: Codealong with Mitch π
Prep for WIP-Work in Progress
π€π½ FeedbackLearning Objectives
Introduction
Almost all professional development involves people with different skills working together to deliver the things their customers want: designers, developers, testers, and so on. How they manage their work together impacts how quickly and effectively they deliver. This exercise explores the importance of ideas like prioritising your backlog, limiting the number of tasks that the team β or parts of a team β does, and reassigning people to work on different types of work to improve workflow.
Notes: Work-in-progress, or WIP, refers to the amount of work in a process that has been started but not completely delivered. This can include both tasks that are actively being worked on, as well as tasks that are waiting in a queue somewhere for the next person to pick up.
Agile Game
π― Goal: Manage a simulated team to deliver as much value as possible to your customer in 4 sprints of 10 working days each. (60 minutes)
Go to https://agilegame.octigo.pl/main and read the rules of the simulation.
When ready to start, click on 2. Play Simulation.
Remember that you can pause the game by toggling the Stop control. This is useful when you want to re-prioritize your backlog, move tasks between different subteams, or move team members between different subteams.
You can also use the Speed control to slow down or speed up the simulation.After 40 simulated days, view your scores. What was the value of the work your team delivered? Consider these questions:
- In what ways did you notice limiting the amount of work-in-progress impact the effectiveness of the subteams?
- How did you prioritize the work your team started on?
- Did you move people between subteams to help when one subteam was overloaded and if so, why did this help?
Do you think you could improve your score? If you want to, you can click 4. Play again to try and improve your results
Post one thing you found most challenging and one thing that was the most fun on your cohortβs CYF Slack channel.
Many development teams adopt this saying:
βStop starting and start finishing!β
Assign teams for Design-A-Snack game
π― Goal: Assign people to teams for the Design-A-Snack game (10 minutes)
- Only ONE person needs to do this prep. Assign people in your cohort into smaller teams of 3-5 people each. If nobody else has announced they are going to do this, send a message on your Slack channel saying you will do it.Β They will surely treat you as the hero that you are and buy you doughnuts.
- Copy this template spreadsheet and fill in the names of you and your colleagues to create an even number of teams of 3-5 people each.Β Decide who will be the facilitator who will tell people when to move on to the next part of the game.
- Post the spreadsheet on your Slack channel so everyone knows which team they are in.
Read 'Design A Snack' instructions
π― Goal: To understand instructions before the class (10 minutes)
Read the instructions of ‘Design-A-Snack game’ so you can do it easily in class.