質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Redux

Reduxは、JavaScriptアプリケーションの状態を管理するためのオープンソースライブラリです。ReactやAngularで一般的にユーザーインターフェイスの構築に利用されます。

バージョン管理

バージョン管理はコンピューター上にファイルとして格納されているドキュメント・プログラム・その他の情報の変更履歴等を管理するものです

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

2回答

1328閲覧

React+Redux+TypeScriptでdataが見つからない

like-here

総合スコア6

Redux

Reduxは、JavaScriptアプリケーションの状態を管理するためのオープンソースライブラリです。ReactやAngularで一般的にユーザーインターフェイスの構築に利用されます。

バージョン管理

バージョン管理はコンピューター上にファイルとして格納されているドキュメント・プログラム・その他の情報の変更履歴等を管理するものです

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2020/12/27 06:10

前提・実現したいこと

React+Redux+TypeScriptでtodo一覧画面を作成中なのですが、下記のエラーメッセージの通り、データが受け取れません。component/task.tsxというファイルでReduxの状態管理をしているのですが、エラーメッセージが表示されているのは親コンポーネントのcomponent/main.tsxのimortしている<Task />の部分で表示されています。
この場合、型の定義等でどこかずれてしまっているか、またはdispatchするところの記述がずれてしまっているか、という仮説を立て、修正してみたのですが、一向に解決できませんでした。。
どなたか、お力添えいただけると大変助かります!

発生している問題・エラーメッセージ

プロパティ 'data' は型 '{}' にありませんが、 型 'Pick<ClassAttributes<Task> & getList, "ref" | "data" | "key">' では必須です。

該当のソースコード

component/task.tsx

TypeScript

1import React, { Component } from "react"; 2import { Button, ButtonGroup, Breadcrumb, Row, Col } from "react-bootstrap"; 3import { Link } from "react-router-dom"; 4import { connect } from "react-redux"; 5import { Dispatch } from "redux"; 6import { readEvents } from "../actions"; 7import { ReduxAction } from "../store"; 8import { State } from "../reducer/events"; 9interface getList { 10 data: { 11 taskId: number; 12 title: string; 13 description: string; 14 status: string; 15 createTime: string; 16 updateTime: string; 17 tag?: string; 18 }[] 19} 20class Task extends React.Component<getList,{}> { 21 render() { 22 return ( 23 <div> 24 <Row className="justify-content-md-center"> 25 <Col xs lg="2"> 26 <Breadcrumb> 27 <ButtonGroup vertical> 28 <Button variant="outline-warning"> 29 <Link to="/task/:id">編集</Link> 30 </Button>{" "} 31 <Button variant="outline-warning"> 32 <Link to="/task/:id">削除</Link> 33 </Button>{" "} 34 </ButtonGroup> 35 </Breadcrumb> 36 </Col> 37 <Col>{this.props.data.title}</Col> 38 <Col>{this.props.data.description}</Col> 39 </Row> 40 </div> 41 ); 42 } 43} 44 45// const mapStateToProps = (state: getList) => { 46// return { 47// value: state.data 48// }; 49// }; 50// const mapDispatchToProps = (dispatch: Dispatch) => { 51// readEvents(); 52// }; 53// export default connect(mapStateToProps, mapDispatchToProps)(Task) 54export class ActionDispatcher { 55 // constructor(private dispatch: (action: ReduxAction) => void) {} 56 constructor(private dispatch: Dispatch<ReduxAction>) {} 57 58 public list() { 59 this.dispatch(readEvents()); 60 } 61} 62export default connect( 63 (state:getList) => ({ value: state.data }), 64 (dispatch: Dispatch<ReduxAction>) => ({ 65 actions: new ActionDispatcher(dispatch), 66 }) 67)(Task);

Action/index.tsx

TypeScript

1import axios from "axios"; 2import { stub } from "./test-stub"; 3import { Action } from "redux"; 4 5// const ROOT_URL = "http://localhost:3000" 6 7//typeの種類 8export const Type = { 9 read: "READ", 10 create: "CREATE", 11 update: "UPDATE", 12 delete: "DELETE", 13} as const; 14 15export type Type = typeof Type[keyof typeof Type]; 16 17//actionの型 18export interface getAction extends Action { 19 type: string, 20 data: { 21 taskId: number; 22 title: string; 23 description: string; 24 status: string; 25 createTime: string; 26 updateTime: string; 27 tag?: string[]; 28 }[]; 29} 30 31 32export const readEvents = (): getAction => ({ 33 type: Type.read, 34 data: stub, 35}); 36 37export type todoActions = getAction

Reducer/events.tsx

TypeScript

1import { todoActions, Type } from "../actions/index"; 2export interface State { 3 type?: string; 4 data: { 5 taskId: number; 6 title: string; 7 description: string; 8 status: string; 9 createTime: string; 10 updateTime: string; 11 tag?: string[]; 12 }[]; 13} 14 15export const initialState: State = { 16 data: [ 17 { 18 taskId: 0, 19 title: "", 20 description: "", 21 status: "", 22 createTime: "", 23 updateTime: "", 24 tag: [], 25 }, 26 ], 27}; 28 29export default function reducer( 30 state: State = initialState, 31 action: todoActions 32) { 33 switch (action.type) { 34 case Type.read: 35 // return Object.assign({}, state, action.data); 36 return {...state,data:action} 37 default: 38 return state; 39 } 40} 41

Reducer/index.tsx

TypeScript

1import { combineReducers } from "redux"; 2import reducer from "./events"; 3import { getAction } from "../actions/index" 4 5export type ReduxState = { 6 reducer: getAction 7} 8 9export default combineReducers({ reducer }); 10

Store/store.tsx

TypeScript

1import { createStore, applyMiddleware } from "redux" 2import reducer from "./reducer" 3import thunk from "redux-thunk"; 4import { Action } from "redux"; 5import { todoActions } from "./actions"; 6 7export const store = createStore(reducer, applyMiddleware(thunk)); 8 9 10export type ReduxAction = todoActions | Action

補足情報(FW/ツールのバージョンなど)

Github
参考資料

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

あれから色々と変えてみて、なんとかdataの更新まで行けました。
お答えして頂いたA_kirisaki様、ありがとうございました。
https://github.com/ykobaya14/dcproject0001/tree/feature/newGetList

投稿2021/01/05 04:23

like-here

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

redux は connect したコンポーネントに state を埋め込みます。

TypeScript

1 (state:getList) => ({ value: state.data }),

の部分ですね。そうするとコンポーネントクラスのメンバー state には data が入ってるはずです。その方が一致しないとまず言っています。それをしているのはどこかというと

TypeScript

1class Task extends React.Component<getList,{}> { 2 // ここ!

です。ここに data の型を入れればよい……のですがあいにく getList の宣言に埋もれてしまっていますので取り出してあげましょう(data 配列を丸々型にするのではなく、小分けにするのがおすすめです)(あと型やインターフェース、クラスの名前は大文字から始めたほうがよいのですが……とりあえずノータッチで)。

TypeScript

1type Data = { 2 taskId: number; 3 title: string; 4 description: string; 5 status: string; 6 createTime: string; 7 updateTime: string; 8 tag?: string; 9} 10 11interface getList { 12 data: Data[] 13}

TypeScript

1class Task extends React.Component<getList,Data[]> {

これで通るでしょう。Typescript + Redux は組み合わせたことはないのでだいぶ霊感で書いてますが、通らなかったらまた質問ください。

投稿2020/12/27 08:22

A_kirisaki

総合スコア2853

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

like-here

2020/12/28 08:09

いつもありがとうございます! 今、回答して頂いた通りに修正をしてみたのですが、同じerror分が表示されました.. この場合は、classにはまた違う形で型の定義をしたほうが良いということなのでしょうか?
A_kirisaki

2020/12/28 20:47

あっ、data って名前特殊だったかもしれない。直してみましょう。
like-here

2020/12/30 06:47

名前変えてみたんですけど、変わらなかったのでactionとかreducerとの型の定義があって無いということでしょうか? 何度もすいません。。
A_kirisaki

2020/12/30 07:29

いえ、こちらこそ一発で答え射抜けずすみません。GitHub のコードを拝見したいのですがプライベートリポジトリでしょうか。可能であればパブリックにしていただきたいのですが。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問