-
Notifications
You must be signed in to change notification settings - Fork 0
Changes #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Building Blocks of
|
| <div className="Box Box--spacious f4"> | ||
| <div className="Box-header"> | ||
| {/* Replace this line with the proper header code*/} | ||
| </div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Step 2: Adding components
Let's add a child component and give our app a header. At the end of the step, your app should like the following:
⌨️ Activity: Add an h3 component to src/App.jsx
- In
src/App.jsx, replace line 92 with this header component:<h3 className="Box-title d-flex flex-justify-center">GradeBook</h3>
- Save your file
- To run the code, move inside the repository directory in your terminal and run
npm start - Exit the process in your terminal using
Ctrl + C - Stage, commit, and push your code to the
changesbranch:git add src/App.jsx git commit -m "add a header component" git push
Watch below this comment for my response.
Interacting with
|
src/App.jsx
Outdated
| @@ -1,33 +1,39 @@ | |||
| import React from "react"; | |||
| import Table from "./Table"; | |||
| /*Add import statement here*/ | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Step 3: Import a Child Component
In this step, we will import the List component that we created for you into App.
⌨️ Activity: Import a List component to src/App.jsx
- At the top of
src/App.jsx, replace line 3 with:import List from "./List";
- Save your file
- To run your code, move inside your repo folder in your terminal and run
npm start - Exit the process in your terminal using
Ctrl + C - Stage, commit, and push your changes to the
changesbranch:git add src/App.jsx git commit -m "import list component" git push
Watch below this comment for my response.
| } | ||
|
|
||
| render() { | ||
| let tabChoice = <div />; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conditional Rendering
Let's take a look at what the code between lines 52 and 61 means. When we click the Assignments button, the list for assignments is assigned to the variable tabChoice. tabChoice is rendered in the render method.
But how do we specialize the List for our assignments? This where our props come into play. We create properties such as placeholder, and currList. We then assign values to them.
Step 4: Import Assignments
Now that we imported the child component, let's use it in our code. At the end of this step, you should be able to click on the Assignments tab and render the following:
⌨️ Activity: Uncomment the assignments conditional in src/App.jsx
- In
src/App.jsx, uncomment the conditional statement between lines 52 and 61 - Save your changes
- To run your code, move inside your repo folder in your terminal and run
npm start - Exit the process in your terminal using
Ctrl + C - Stage, commit, and push your changes to the
changesbranch:git add src/App.jsx git commit -m "uncomment assignment rendering" git push
Watch below this comment for my response.
| return ( | ||
| <div className="col-6 mx-auto"> | ||
| <p className="h2">REPLACE THIS TITLE WITH A PROP</p> | ||
| {/*Replace the code below to call the title prop*/} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Step 5: Adding a prop
What does the List component do with the information we just included? Adding this.props tells the component to look for the property that was passed to it.
To see, open the src/List.jsx file. In the render method, in our input tag, notice that we set our placeholder to this.props.placeholder. Then, at the bottom of our render method, we loop through our this.props.currList. This shows each item in the list that we pass.
Let's go ahead and replace the current title with another prop.
⌨️ Activity: Replace the title prop in src/List.jsx
- In
src/List.jsx, replace line 31 with{this.props.title} - Save your changes
- To run your code, move inside your repo folder in your terminal and run
npm start - Exit the process in your terminal using
Ctrl + C - Stage, commit, and push your changes to the
changesbranch:git add src/List.jsx git commit -m "use a prop for the header" git push
Watch below this comment for my response.
State VariablesSo now, we learned how to pass data to components, but let's talk about the data itself. There are two types of data: static data and dynamic data. Static data doesn't change. Things like a title on a page or the name of a button would be static data. Dynamic data, on the other hand, is data that does change. In our example, we can change it by adding an item to the list. Our list of assignments would also be considered dynamic data. But, how do we deal with dynamic data in React? Scroll below to take a look in the code! |
| this.state = { | ||
| buttonClicked: "", | ||
| assignments: [], | ||
| grades: {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
State variables
In React, we store dynamic data in state variables. Let's take a look at how we store state variables. In src/App.jsx, take a look at the constructor. You'll see that we declare our state variables using this.state = {.....}.
Currently, in App, we have three state variables:
buttonClicked- This stores which button was clicked. It is a state variable because the button that is clicked has the ability to change.assignments- This stores the list of assignments. It is a state variable because the list changes every time a new assignment is added.grades- This should store the grade for each student. However, we have no way to store students, so let's fix that!
Step 6: Add a state variable
Let's add a way to store students as a variable.
⌨️ Activity: Add a state variable to src/App.jsx
- On line 11 of
src/App.jsx, add a state variable namedstudents - Set
studentsequal to an empty array - Add a comma:
students: [], - Save your changes
- To run your code, move inside your repo folder in your terminal and run
npm start - Exit the process in your terminal using
Ctrl + C - Stage, commit, and push your changes to the
changesbranch:git add src/App.jsx git commit -m "add a state variable for students" git push
Watch below this comment for my response.
|
It looks like you did not add a Try again! If you would like assistance troubleshooting the issue you are encountering, create a post on the GitHub Community board. You might also want to search for your issue to see if other people have resolved it in the past. |
| }); | ||
| } | ||
|
|
||
| /*Check out this addAssignment method*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explaining this.setState
How do we change data within a state variable?
Unfortunately, we can't just do this.state.studentsList = ..... to change data.
To set the state of a state variable we have to use the method, this.setState.
Take a look at the method on line 30 in src/App.jsx
When this method is called, it is adding assignmentName to our state variable assignments by setting the state of assignments to itself with the concatenation of assignmentName.
We are going to set some state in next steps.
| assignments: this.state.assignments.concat(assignmentName) | ||
| assignments: this.state.assignments.concat(assignmentName), | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Step 7: Create a method
Now that we see how it works with assignments, let's try it with students! We will create an addStudent method to add students the same way we add assignments.
⌨️ Activity: Create an addStudent method in src/App.jsx
- On line 37 of
src/App.jsx, create a method calledaddStudentbelow the commentaddStudentshould takestudentNameas a parameteraddStudentshould concatstudentNameto the end ofstudentslist that we created earlier- Hint: You can copy the same format for the
addAssignmentmethod
- Save your changes
- To run your code, move inside your repo folder in your terminal and run
npm start - Exit the process in your terminal using
Ctrl + C - Stage, commit, and push your changes to the
changesbranch:git add src/App.jsx git commit -m "create addStudent method" git push
Watch below this comment for my response.
|
|
||
| this.handleButtonClicked = this.handleButtonClicked.bind(this); | ||
| this.addAssignment = this.addAssignment.bind(this); | ||
| /*Uncomment the line below to bind the method*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Step 8: Binding a method
Since we created a method that changes a state variable, we have to bind it to our class. Then, when we call it, we know what method to reference.
⌨️ Activity: Bind addStudent to App
- Uncomment line 18 of
src/App.jsxto bindaddStudenttoApp - Save your changes
- To run your code, move inside your repo folder in your terminal and run
npm start - Exit the process in your terminal using
Ctrl + C - Stage, commit, and push your changes to the
changesbranch:git add src/App.jsx git commit -m "bind addStudent to App" git push
Watch below this comment for my response.
Callback FunctionsThe last thing you need to know before you can call yourself a React Developer is callback functions. We said that we can pass data to the child with With callback functions, we are able to do just that. We pass a function as a prop, and then the child component can call the function that was defined in the parent. Let's look at an example below. |
| let tabChoice = <div />; | ||
|
|
||
| /*Uncomment below to render assignments*/ | ||
| if (this.state.buttonClicked === "assignments") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example of passing functions as props
So where exactly are those functions we created to set state getting called?
On line 63 in src/App.jsx, it looks like we pass the addAssignment function as a prop in this chunk of code.
Navigate to src/List.jsx and take a look at the code between lines 18 and 25 in src/List.jsx.
handleSubmit(event) {
this.setState({
value: ""
});
this.props.addFunction(this.state.value);
event.preventDefault();
}When the submit button is clicked, we call the addFunction with the value of our input box. For assignments, the addFunction references the this.addAssignment in App. So when we call this.props.addFunction, we are calling back to the parent component and using the parent's function.
Scroll below for next steps.
src/App.jsx
Outdated
| /*if (this.state.buttonClicked === "students") { | ||
| tabChoice = ( | ||
| <List | ||
| placeholder="Add Assignment..." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Step 9: Adding the correct props
There are some issues with this code. We want to be modifying the students list, not the assignments list.
Let's change some props between lines 71 and 80 to render the correct components when we click Students.
⌨️ Activity: Change the props for Students
- Uncomment the conditional statement between lines 65 and 75 of
src/App.jsx - On line 68, change the
placeholderprop to"Add Student..." - On line 69, change the
currListprop to{this.state.students} - On line 70, change the
addFunctionprop to{this.addStudent} - Save your changes
- To run your code, move inside your repo folder in your terminal and run
npm start - Exit the process in your terminal using
Ctrl + C - Stage, commit, and push your changes to the
changesbranch:git add src/App.jsx git commit -m "change props for students rendering" git push
Watch below this comment for my response.
|
It looks like you did not replace the props for student rendering. Try again! If you would like assistance troubleshooting the issue you are encountering, create a post on the GitHub Community board. You might also want to search for your issue to see if other people have resolved it in the past. |
|
|
||
| /* Uncomment lines below to render grades*/ | ||
| /*if (this.state.buttonClicked === "grades") { | ||
| tabChoice = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Step 10: Include Grades
Let's get our grades working! At the end of this activity, your code should now be able to add assignments, students, and grades.
⌨️ Activity: Uncomment the grades conditional in src/App.jsx
- In the
src/App.jsxfile, uncomment the conditional statement between lines 77 and 86 for ourgradestab - Save your changes
- To run your code, move inside your repo folder in your terminal and run
npm start - Exit the process using
Ctrl + C - Stage, commit, and push your changes to the
changesbranch:git add src/App.jsx git commit -m "uncomment grades section" git push
Watch below this comment for my response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Step 11: Merge your pull request
Now that we've approved the changes, it's time to merge the pull request.
⌨️ Activity: Merge the changes branch into master
- Click the
Merge Pull Requestbutton
Watch below this comment for my response.
Congratulations!Congratulations on finishing Introduction to React. To update your local course, use Although this course certainly doesn't mean you are a React expert, we hope you have the tools to explore further content on your own. Here are some cool resources for further learning: |




No description provided.