forked from AdaGold/madlib
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathApp.js
48 lines (41 loc) · 1.22 KB
/
App.js
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
import React, { Component } from 'react';
import './App.css';
import MadLibs from './madlibs/MadLibs.js';
import Story from './components/Story.js';
import NewMadLibsForm from './components/NewMadLibsForm';
class App extends Component {
constructor() {
super();
this.state = {
selectedMadLib: MadLibs[Math.floor(Math.random() * (MadLibs.length-1))],
};
}
// Update the value of a word in the selected
// mad lib using setState
updateWord = (key, value) => {
const updatedMadLib = this.state.selectedMadLib;
const changedWord = updatedMadLib.words.find((word) => {
return word.key === key
});
changedWord.value = value;
this.setState({selectedMadLib: updatedMadLib});
}
render() {
console.log(this.state.selectedMadLib);
return (
<section className="App">
<h1>Welcome to MadLibs!</h1>
<p>Fill in all of the choices to see your final story.</p>
<NewMadLibsForm
words = {this.state.selectedMadLib.words}
updateWord = {this.updateWord}
/>
<Story
title={ this.state.selectedMadLib.title }
text={ this.state.selectedMadLib.getText() }
/>
</section>
);
}
}
export default App;