-
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
Changes #2
Changes from all commits
a257943
1e8a713
acb1b15
0f2cc53
e25ccee
724a880
f3d2353
5536c12
5c10e1b
0ea3713
b6ad27f
0478965
c7760be
cc85d8b
3cafd73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,42 @@ | ||
| import React from "react"; | ||
| import Table from "./Table"; | ||
| import List from "./List"; | ||
|
|
||
| class App extends React.Component { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| buttonClicked: "", | ||
| assignments: [], | ||
| grades: {} | ||
| assignments: [] /*Below this line, add the students state variable*/, | ||
| students: [], | ||
| grades: {}, | ||
| }; | ||
|
|
||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. Step 8: Binding a methodSince 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
|
||
| this.addStudent = this.addStudent.bind(this); | ||
| this.addGrade = this.addGrade.bind(this); | ||
| } | ||
|
|
||
| handleButtonClicked(buttonName) { | ||
| this.setState({ | ||
| buttonClicked: buttonName | ||
| buttonClicked: buttonName, | ||
| }); | ||
| } | ||
|
|
||
| /*Check out this addAssignment method*/ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explaining
|
||
| addAssignment(assignmentName) { | ||
| this.setState({ | ||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. Step 7: Create a methodNow that we see how it works with assignments, let's try it with students! We will create an ⌨️ Activity: Create an
|
||
|
|
||
| /*Write an addStudent method here*/ | ||
| addStudent(studentName) { | ||
| this.setState({ | ||
| assignments: this.state.students.concat(studentName), | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -42,10 +54,48 @@ class App extends React.Component { | |
| render() { | ||
| let tabChoice = <div />; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conditional RenderingLet's take a look at what the code between lines 52 and 61 means. When we click the But how do we specialize the Step 4: Import AssignmentsNow 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 ⌨️ Activity: Uncomment the assignments conditional in
|
||
|
|
||
| /*Uncomment below to render assignments*/ | ||
| if (this.state.buttonClicked === "assignments") { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example of passing functions as propsSo where exactly are those functions we created to set state getting called? On line 63 in Navigate to handleSubmit(event) {
this.setState({
value: ""
});
this.props.addFunction(this.state.value);
event.preventDefault();
}When the submit button is clicked, we call the Scroll below for next steps. |
||
| tabChoice = ( | ||
| <List | ||
| placeholder="Add Assignment..." | ||
| currList={this.state.assignments} | ||
| addFunction={this.addAssignment} | ||
| title="Assignments" | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| /* Change below to render students*/ | ||
|
|
||
| if (this.state.buttonClicked === "students") { | ||
| tabChoice = ( | ||
| <List | ||
| placeholder="Add Student..." | ||
| currList={this.state.students} | ||
| addFunction={this.addStudent} | ||
| title="Student Roster" | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| /* 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 commentThe reason will be displayed to describe this comment to others. Learn more. Step 10: Include GradesLet'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
|
||
| <Table | ||
| tableNames={this.state.assignments} | ||
| rows={this.state.students} | ||
| addFunction={this.addGrade} | ||
| data={this.state.grades} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div> | ||
| <div className="Box Box--spacious f4"> | ||
| <div className="Box-header"> | ||
| <h3 className="Box-title d-flex flex-justify-center">GradeBook</h3> | ||
| </div> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Step 2: Adding componentsLet'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
|
||
| </div> | ||
| <nav className="UnderlineNav d-flex flex-justify-center"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ class List extends React.Component { | |
| constructor(props) { | ||
| super(props); | ||
| this.state = { | ||
| value: "" | ||
| value: "", | ||
| }; | ||
|
|
||
| this.handleChange = this.handleChange.bind(this); | ||
|
|
@@ -17,7 +17,7 @@ class List extends React.Component { | |
|
|
||
| handleSubmit(event) { | ||
| this.setState({ | ||
| value: "" | ||
| value: "", | ||
| }); | ||
|
|
||
| this.props.addFunction(this.state.value); | ||
|
|
@@ -27,7 +27,8 @@ class List extends React.Component { | |
| render() { | ||
| 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 commentThe reason will be displayed to describe this comment to others. Learn more. Step 5: Adding a
|
||
| <p className="h2">{this.props.title}</p> | ||
| <form onSubmit={this.handleSubmit}> | ||
| <label> | ||
| <input | ||
|
|
||


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 usingthis.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.jsxsrc/App.jsx, add a state variable namedstudentsstudentsequal to an empty arraystudents: [],npm startCtrl + Cchangesbranch:Watch below this comment for my response.