質問するログイン新規登録

質問編集履歴

1

ご指摘のあった箇所を修正してたソースコードを追加しました。

2021/01/10 23:37

投稿

Kentarou3
Kentarou3

スコア1

title CHANGED
File without changes
body CHANGED
@@ -110,6 +110,108 @@
110
110
  </html>
111
111
  ```
112
112
 
113
+ 2021/1/11修正版
114
+ ```React
115
+
116
+ <!DOCTYPE html>
117
+ <html>
118
+
119
+ <head>
120
+ <meta charset="UTF-8" />
121
+ <title>React</title>
122
+ <script src="https://unpkg.com/react@17/umd/react.development.js"></script>
123
+ <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
124
+ <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
125
+ </head>
126
+
127
+ <body>
128
+ <div id="root"></div>
129
+ <script type="text/babel">
130
+
131
+ (() => {
132
+
133
+ class App extends React.Component {
134
+ constructor() {
135
+ super();
136
+ this.state = {
137
+ answer: "abc",
138
+ blank: "a__",
139
+ location: 1,
140
+ speaking: undefined,
141
+ }
142
+ this.keydown = this.keydown.bind(this)
143
+ this.speak = this.speak.bind(this)
144
+ }
145
+ speak(word) {
146
+ var speech = new SpeechSynthesisUtterance(word)
147
+ speechSynthesis.speak(speech)
148
+ console.log(speechSynthesis)
149
+ this.setState({
150
+ ...this.state,
151
+ speechSynthesis
152
+ })
153
+ speech.onend = function (event) {
154
+ console.log(event)
155
+ console.log(speechSynthesis)
156
+ }
157
+ }
158
+ keydown(e) {
159
+ console.log(e.key)
160
+
161
+ if (this.state.answer[this.state.location] == e.key) {
162
+ this.setState((state, props) => ({
163
+ location: state.location + 1
164
+ }));
165
+ let blank = this.state.answer.substring(0, this.state.location) + '_'.repeat(this.state.blank.length - this.state.location);
166
+
167
+ this.setState({
168
+ blank: blank
169
+ })
170
+ console.log(blank)
171
+
172
+ if (this.state.answer.length == this.state.location) {
173
+ this.speak(this.state.answer)
174
+ console.log(this.state.speechSynthesis)
175
+ // while (this.state.speechSynthesis.speaking)
176
+ this.setState({
177
+ ...this.state,
178
+ answer: "def",
179
+ blank: "d__",
180
+ location: 1,
181
+ })
182
+ }
183
+ }
184
+ }
185
+
186
+ componentDidMount() {
187
+ window.addEventListener('keydown', this.keydown)
188
+ }
189
+
190
+ render() {
191
+ return (
192
+ <div>
193
+ <div>
194
+ {this.state.answer}
195
+ </div>
196
+ <div>
197
+ {this.state.blank}
198
+ </div>
199
+ </div>
200
+ );
201
+ }
202
+ }
203
+
204
+ ReactDOM.render(
205
+ <App />,
206
+ document.getElementById('root')
207
+ );
208
+ })();
209
+
210
+ </script>
211
+ </body>
212
+
213
+ </html>
214
+ ```
113
215
  ### 試したこと
114
216
 
115
217
  componentDidUpdateとフラグの処理などを使って試してみましたが、どのようにしても思うような結果になりません。。