forked from itscodenation/flw2-trivia-20-21-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.mdma
238 lines (204 loc) · 7.87 KB
/
README.mdma
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Trivia App!
## Project Description
For this project we will be building a trivia app. This project will use firebase to fetch questions and react to diplay questions and answers.
====
### Day 1 Setup and Intro to React
Goal: Set up your project and view the live running app.
- [ ] Use your [Outline and Planning Doc](https://docs.google.com/document/d/1oiyYdTcO2RxbE-2yq5KmeZpthExzHCNrgrVGtT47yOg/edit) plan your project.
- [ ] Go to the repository at https://github.com/ScriptEdcurriculum/trivia
- [ ] Fork the repository to your github account and clone to a new workspace
- [ ] Add, commit, and push your changes
- [ ] To set up your app, in your terminal type `npm install`
- [ ] To run your app, in your terminal type `npm start` this runs the app in the development mode. Then open [http://localhost:3000](http://localhost:3000) to view it in the browser.
- [] Use any remaining time to style your app in App.css
====
### Day 2 Creating Components and Component Layout
Goal: Set up your your components
- [ ] In your App.jsx file import the Question Component
- [ ] Inside the render function call your Question component
- [ ] Using the Question.jsx as a template create three more components called AnswerButton.jsx, QuestionText.jsx and ResetButton.jsx
- [ ] In your Question.jsx file import your QuestionText, AnswerButton and ResetButton. Call each of the components in the render function. (You should have 4 AnswerButtons)
- [ ] The QuestionText should render a div with the text of any question of your choosing
- [ ] The AnswerButton should should render a div with the text of any question of your choosing
- [ ] The ResetButton should should render a div with the text "Reset"
- [ ] Style these in any way you choose.
### Day 3 Passing Down Props to components
- [ ] Now that we have this, we can pass down the questions to other components using props. In your App.jsx let add a prop to the Question component and pass it the currentQuestion from state.
```
< Question
questionText={"What is the answer to the Ultimate Question of Life"}
/>
```
- [ ] Now we can access this information in the Question component using props. Then we can pass down currentQuestion's question to the QuestionText component. Open Question.jsx and add the following.
```
< QuestionText
questionText={this.props.questionText}
/>
```
- [ ] Finally in the QuestionText component lets display that question.
```
<div className="questionText">
{this.props.questionText}
</div>
```
- [ ] Now lets do the same process to pass down your choices to each AnswerButton and diplay the answer choices.
- [ ] Use your remaining time to style your project
### Day 4 Updating State
Now that we have props that we can pass down we need a way to store/ keep track of props if we change them. Thats where we can use state.
- [ ] In your App.jsx file lets add a constructor method directly inside your App component
```
class App extends Component {
constructor(props){
super(props);
this.state= {
}
}
```
- [ ] Since we want to keep track of the question each time the page loads lets add a property to the state to keep track of the state. We want to keep track of all the questions and the current displayed function. Complete the rest of the state to match the structure of the question.
```
this.state= {
questions: {},
currentQuestion: {
question_text: "Question",
choices: [],
correct_choice_index: null,
}
}
```
- [ ] Now we can access the question_text from state and pass it down to each subsequent component. Notice your question text change.
```
<Question
questionText={this.state.currentQuestion.question_text}
/>
```
- [ ] Follow the same pattern to pass down state to each of your answer choices.
- [ ] Last we want to grab a new question when the page loads we need to write a firebase function in the the constuctor.
```
constructor(props) {
super(props);
firebaseDatabase.ref('/questions').on('value', (snapshot)=> {
console.log(snapshot.val())
});
```
- [ ] use this.setState to change the state.
```
firebaseDatabase.ref('/questions').on('value', (snapshot)=> {
let questions = snapshot.val();
let randomQuestion = getRandomQuestion(questions)
this.setState({
questions: questions,
currentQuestion: randomQuestion,
})
});
```
- [ ] Reload the page to see your conent change.
### Day 5 Reacting to user click
- [ ] Create a new function that when you click on an AnswerButton component changes the questionText to display the correct answer.
```
_onAnswerButtonClicked(){
this.setState({
shouldDisplayAnswer: true,
})
}
```
- [ ] Now we need to pass this function down through props to the button component. We do this using and arrow function displayed below.
```
<Question
...
answerButtonClicked={()=>this._onAnswerButtonClicked()}
/>
```
- [ ] In Question.jsx keep passing it down through props
```
<AnswerButton
answerButtonClicked={this.props.answerButtonClicked}
/>
```
- [ ] In AnswerButton.jsx we can add it to the onClick function
```
return (
<div className="answer-button"
onClick={this.props.onAnswerButtonClicked()}
>
{answerText}
</div>
);
```
- [ ] Lastly we can use the property shouldShowCorrectAnswer to tell our QuestionText what to display.
```
<Question
...
shouldShowCorrectAnswer={shouldShowCorrectAnswer}
correctAnswer={question.choices[question.correct_choice_index]}
/>
```
- [ ] Lastly we can use the property shouldDisplayAnswer to tell our QuestionText what to display.
```
let display;
if(shouldShowCorrectAnswer){
display = "The correct answer is " + correctAnswer;
} else {
display = questionText;
}
return (
<div className="questionStem">
{display}
</div>
);
```
### Day 6 More Functions and Conditional Rendering
- [ ] In your App.jsx create a method function call _onResetButtonClicked(). When this function is called it will set the current question to a new question.
```
_onResetButtonClicked(){
let randomQuestion = getRandomQuestion(this.state.questions)
this.setState({
currentQuestion: randomQuestion,
shouldShowCorrectAnswer: false,
})
}
```
- [ ] Now we need to pass this function down through props to the button component. We do this using and arrow function displayed below.
```
<Question
...
resetButtonClicked={()=>this.onResetButtonClicked()}
/>
```
- [ ] In Question.jsx keep passing it down through props
```
<ResetButton
resetButtonClicked={this.props.resetButtonClicked}
/>
```
- [ ] In AnswerButton.jsx we can add it to the onClick function
```
return (
<div className="reset-button"
onClick={this.props.resetButtonClicked}
>
Reset
</div>
);
```
### Day 7 Work day and making your site live
- [ ] Make your site live on gh-pages
- [ ] npm install gh-pages --save-dev
- [ ] open your package.json file and add both of these
```
"homepage": "http://gitname.github.io/react-gh-pages"
```
```
"scripts": {
//...
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}
```
- [ ] npm run deploy
### Day 8 Presentation Preperation Day
- [] Using the [Presentaion Guidelines](https://docs.google.com/document/d/1ot54zTTJo7m7dMaN-yTZH6Y-kymEyNSJ4jLzNwLuskg/edit) and [Pitch Rubric](https://docs.google.com/document/d/1an_aanEdOoYftxjqcGB-0IxkW2BVGY5sH5SlJv9weBU/edit)
### Extensions!
- [] Make a timer that resets the game when the timer runs out
- [] Make a counter that keeps track of how many times you've guessed the correct answer
- [] Change the color of the answer buttons when the user guesses. For example turn the button with the correct answer to green.
- [] Anything else you want!