React: Ideabox Review
Learning Goals
- Dive into the Ideabox activity to clarify any complex topics and highlight important React concepts
- Understand how to start a React project by seeing the instructor model the wireframing process
- Gain comfort in using React’s Dev Tools to better support you as you write React code
- Explain the importance of controlled forms in React
- Talk through the topics of anonymous functions, destructuring, and conditional styling
Vocab
wireframe
- a wireframe is a visual representation of the basic structure of a page, without worrying too much about specific content or design detailsanonymous function
- a function that does not have a namedestructuring
- a way to unpack values from arrays, or properties from objects, into distinct variablescontrolled form
- a form written so that each input’s value is stored in a component’s state and the value shown on the page is pulled from state
Set Up
This lesson is a review session for the Ideabox Tutorial lesson. If you did not complete all of the steps on that tutorial, you can clone down a completed version here: https://github.com/turingschool-examples/react-ideabox
Follow the instructions for cloning, installing, and running the app. You do not need to follow any steps for a running server.
What’s an Ideabox, anyway?
IdeaBox is a Turing term meant to describe a simple application that allows a user to fill out a form and then display that information. If you think about it, there are LOTS of “IdeaBoxes” out there: - Facebook: users fill out a form and their post appears - Google: users fill out a form and search results appear - Amazon: users search for an item and a list of items appear
If you can build lots of versions of IdeaBoxes in M3, you’ll be well equipped to build pretty much anything you’re asked to. You can find inspo for those IdeaBoxes here!
Wireframing + Planning
Let’s go back to the beginning. One of the most important parts of creating a React application is being able to visualize the components on the page. I am going to model for you how I would turn a concept into React components.
For this process, you may choose to use an iPad, a tool like Figma or Mockflow, or paper and pencil. What matters is that you’re able to turn a wireframe into a plan for your component architecture.
Let’s work out the following:
-
What components should we create?
-
How will those components be related to each other? (Draw out your component tree)
-
Which components should hold state? What data will be held in state?
-
Which components will hold methods that affect our data?
-
What props will we need to pass around?
Now that we’ve got a general plan, let’s consider these questions:
-
Why can’t we just pass the ideas between the
Form
andIdeas
components? -
Where should
addIdea
live? It will be triggered by the button inForm
, but it will update the state living inApp
. Which component should contain that method? -
Will we ever pass props to
App
? Why or why not? -
Is there a way to pass a prop directly to the desired child, or do you have to pass to every child component down the tree to get to where you want?
React Dev Tools
Let’s take a minute to look at the React Dev Tools. If you haven’t already, install the dev tools Chrome extension now.
Run your Ideabox in the browser and open up your dev tools. Navigate to the “Components” tab. Answer the following questions:
-
What props are being passed to the Form component? What component is passing those props?
-
What props are being passed to the Ideas component? What component is passing those props?
-
When you add a new idea, what component is created? What props is it being passed?
-
When you add a new idea, what component(s) have a state change?
-
As you fill out the form, where can you see the form values being stored in state?
-
Do you think this means we no longer need to
console.log
?
Controlled Forms
Now that we’ve seen what the Form’s state looks like in the dev tools, let’s talk a bit more about controlled forms. From the React docs:
“In HTML, form elements such as input, textarea, and select typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated with setState().
We can combine the two by making the React state be the ‘single source of truth’. Then the React component that renders a form also controls what happens in that form on subsequent user input. An input form element whose value is controlled by React in this way is called a “controlled component”.”
Basically, we want our React state to be our single source of truth when it comes to what the user is seeing. We want to make sure that every update made on the page is logged in our state. We can write our forms so that every keystroke triggers a state update and (as always) every state update triggers an update on the screen. To the user, there is no difference. But to the developer, we now have much more control over the data.
Consider the following questions:
-
In your own words, what is a controlled form and why do we use them in React?
-
How does using a controlled form help us with form validation as frontend devs?
Anonymous Functions
Consider this code:
<button onClick={() => deleteIdea(id)}>🗑</button>
Why does deleteIdea(id)
need to be in an anonymous function? Why not just write this:
<button onClick={deleteIdea(id)}>🗑</button>
-
What is an anonymous function and when are they useful?
-
Can you think of an example from Mod 2 where we needed to use anonymous functions?
Destructuring
What is happening on this line of code:
const Ideas = ({ ideas, deleteIdea }) => {
What changes would need to be made to the rest of the code in this file if we had written that line like this:
const Ideas = (props) => {
Destructuring is confusing. Do I have to do it? Nope! In fact if this is what is making things confusing for you, don’t do it (yet). Focus on understanding React and THEN start trying to refactor using destructuring. It’ll be important that destructuring makes sense to you by the end of this inning, as it’s a common way for code to be written, but it’s more important that you understand React right now. If you’d like some practice, check out this replit!
Conditional Styling
The Ideabox you built didn’t really have an opportunity for conditional styling. Let’s look at the code below:
// some React component
[status, setStatus] = useState('valid')
//...
<p className={status}>Some text</p>
//...
/* CSS file */
.valid {
color: green;
}
.invalid {
color: red;
}
-
Explain what is happening here.
-
What happens to the paragraph text if the following code runs:
setStatus('invalid')
Questions
Wow! We covered a lot. What other parts of the Ideabox exercise would you like to talk about?