質問編集履歴

1

なぜか文章が途中で抜けていたので再度書き直しました。

2019/10/09 17:50

投稿

southernX
southernX

スコア5

test CHANGED
File without changes
test CHANGED
@@ -20,11 +20,13 @@
20
20
 
21
21
  ![イメージ説明](8ed0635ce1811801109482256b40e1ea.png)
22
22
 
23
+ Countは今のところh1要素をレンダリングしているだけです。
24
+
23
25
  ### 発生している問題・エラーメッセージ
24
26
 
25
27
 
26
28
 
27
- サインインページのログインボタンを押すと問題なく/countのurlに変わるのですが表示されるコンポーネント変わりません。しかしブラウザで再読み込みすればCountコンポーネントが表示されます
29
+ サインインページのログインボタンを押すと問題なく/countのurlに変わるのですが表示されるコンポーネント変わりません。しかしブラウザで再読み込みすればCountコンポーネントが表示されます
28
30
 
29
31
 
30
32
 
@@ -124,6 +126,156 @@
124
126
 
125
127
  ```
126
128
 
129
+ Appコンポーネントはurlが/signinの時にSigninコンポーネントを、/countの時にCountコンポーネントをレンダリングします。
130
+
131
+
132
+
133
+ ```Signintsx
134
+
135
+ import * as React from 'react';
136
+
137
+ import { connect } from 'react-redux'
138
+
139
+ import { logInIfFetchUser } from '../modules/ActionCreater'
140
+
141
+ import { AppState } from '../modules/Reducers'
142
+
143
+
144
+
145
+
146
+
147
+ type SignInProps = {
148
+
149
+ logInIfFetchUser: any
150
+
151
+ }
152
+
153
+ class SignIn extends React.Component<SignInProps> {
154
+
155
+ render(){
156
+
157
+ let email: any
158
+
159
+ let password: any
160
+
161
+
162
+
163
+ return(
164
+
165
+ <div>
166
+
167
+ <h1>サインイン</h1>
168
+
169
+ <form onSubmit={(e) => {
170
+
171
+ e.preventDefault()
172
+
173
+ console.log(`searching user with email: ${email.value}, password: ${password.value}`)
174
+
175
+ this.props.logInIfFetchUser(email.value, password.value)
176
+
177
+ }}>
178
+
179
+ <input type='text' ref={node => email = node}/>
180
+
181
+ <input type='password' ref={node => password = node}/>
182
+
183
+ <button type='submit'>ログイン</button>
184
+
185
+ </form>
186
+
187
+ </div>
188
+
189
+ )
190
+
191
+ }
192
+
193
+ }
194
+
195
+
196
+
197
+ const mapStateToProps = (state: AppState) => {
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+ export default connect(mapStateToProps,{ logInIfFetchUser })(SignIn)
206
+
207
+
208
+
209
+ ```
210
+
211
+ SignInコンポーネントです。
212
+
213
+ onSubmitした時のアクションが以下の通りです。
214
+
215
+
216
+
217
+ ```ActionCreatorts
218
+
219
+ import axios from 'axios'
220
+
221
+ import { Action } from 'redux'
222
+
223
+ import { ThunkAction } from "redux-thunk"
224
+
225
+ import { push } from 'connected-react-router'
226
+
227
+ import { LOGIN } from './ActionTypes'
228
+
229
+ import { AppState } from './Reducers'
230
+
231
+
232
+
233
+ export function logIn(userName: string) {
234
+
235
+ return {
236
+
237
+ type: 'LOGIN' as typeof LOGIN,
238
+
239
+ name: userName
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+
247
+
248
+
249
+ export const logInIfFetchUser = (
250
+
251
+ email: string,
252
+
253
+ password: string
254
+
255
+ ): ThunkAction<void, AppState, null, Action<string>> => async dispatch => {
256
+
257
+ axios.get(`http://localhost:8080/api/?email=${email}&password=${password}`)
258
+
259
+ .then((response) => {
260
+
261
+ dispatch(logIn(response.data[0].name))
262
+
263
+ dispatch(push('/count'))
264
+
265
+ })
266
+
267
+ }
268
+
269
+
270
+
271
+ ```
272
+
273
+ SignInで入力されたアドレスとパスワードと一致するユーザーをapiサーバーから返します。
274
+
275
+ その後、/countに移動します。
276
+
277
+ リクエストは成功し、ユーザーが返ってきたことは確認しました。
278
+
127
279
 
128
280
 
129
281
 
@@ -134,12 +286,144 @@
134
286
 
135
287
 
136
288
 
289
+ connect()(SignIn)をさらにwithRouter()でラップしてみました。
290
+
291
+
292
+
137
- ここ問題に対ししたことを記載してください
293
+ connect()の引数を以下のよう変えみました。
294
+
138
-
295
+ ```
296
+
139
-
297
+ export default connect(mapStateToProps, { logInIfFetchUser }, null, {
298
+
140
-
299
+ pure: false
300
+
301
+ })(SignIn)
302
+
303
+ ```
304
+
305
+
306
+
141
- ### 補足情報(FW/ツーのバージョンなど
307
+ ## その他のファイ(ReducerやStoreなど)
308
+
142
-
309
+ ```Reducerts
310
+
143
-
311
+ import { Action, LOGIN } from './ActionTypes'
312
+
144
-
313
+ import { Reducer, combineReducers } from 'redux'
314
+
315
+ import { History } from 'history'
316
+
317
+ import { connectRouter } from 'connected-react-router'
318
+
319
+
320
+
321
+ type userReducerState = {
322
+
323
+ name: string
324
+
325
+ }
326
+
327
+
328
+
329
+ const initialState = {
330
+
331
+ name: ''
332
+
333
+ }
334
+
335
+
336
+
337
+ export const userReducer: Reducer<userReducerState, Action> = (state = initialState, action) => {
338
+
145
- ここにより詳細な情報を記載してください。
339
+ switch(action.type) {
340
+
341
+ case LOGIN:
342
+
343
+ return {
344
+
345
+ name: action.name
346
+
347
+ }
348
+
349
+ default:
350
+
351
+ return state;
352
+
353
+ }
354
+
355
+ }
356
+
357
+
358
+
359
+ export type userReducer = typeof userReducer
360
+
361
+
362
+
363
+ export const rootReducer = (history: History) => combineReducers({
364
+
365
+ router: connectRouter(history),
366
+
367
+ userReducer
368
+
369
+ })
370
+
371
+
372
+
373
+ export type AppState = ReturnType<typeof rootReducer>
374
+
375
+
376
+
377
+ ```
378
+
379
+ ```Storets
380
+
381
+ import { createStore } from 'redux'
382
+
383
+ import { applyMiddleware } from "redux";
384
+
385
+ import thunk from 'redux-thunk';
386
+
387
+ import { createBrowserHistory } from 'history'
388
+
389
+ import { routerMiddleware } from 'connected-react-router'
390
+
391
+ import { rootReducer } from "./Reducers";
392
+
393
+
394
+
395
+ export const history = createBrowserHistory()
396
+
397
+
398
+
399
+ export const store = createStore(
400
+
401
+ rootReducer(history),
402
+
403
+ applyMiddleware(
404
+
405
+ routerMiddleware(history),
406
+
407
+ thunk
408
+
409
+ )
410
+
411
+ )
412
+
413
+
414
+
415
+ ```
416
+
417
+ ```ActionCreator
418
+
419
+ import { logIn } from './ActionCreater'
420
+
421
+
422
+
423
+ export const LOGIN = 'LOGIN'
424
+
425
+ export type Action = ReturnType<typeof logIn>
426
+
427
+ ```
428
+
429
+ userReducerはまだ使っていません。後にユーザー名を取り出すつもりです。