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

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

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

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

TypeScript

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

React.js

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

Q&A

解決済

1回答

4198閲覧

typescriptでreduxのstateにアクセス可能でかつreact-routerのRouteに指定できるコンポーネントを作りたい

m0a

総合スコア708

Redux

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

TypeScript

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

React.js

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

0グッド

0クリップ

投稿2017/07/05 06:52

まずはコードをご確認下さい

typescript:App.tsx

1import * as React from 'react'; 2import {BrowserRouter as Router, Route, Link, withRouter} from 'react-router-dom'; 3import {RouteComponentProps} from 'react-router'; 4import {connect} from 'react-redux'; 5 6interface TestProps { 7 state: Object; 8} 9const Index = (props: RouteComponentProps<undefined>) => (<h1>index</h1>); 10const About = (props: RouteComponentProps<undefined>) => (<h1>about</h1>); 11 12const AccessReduxStore = (props: RouteComponentProps<{}> & TestProps) => (<h1>access to redux store</h1>); 13const mapStateToProps = (state: {}): TestProps => { 14 return { 15 state 16 }; 17}; 18 19const ReduxAccessWithRouter = withRouter<TestProps>(connect(mapStateToProps)(AccessReduxStore)); 20 21class App extends React.Component<{}, {}> { 22 render() { 23 return ( 24 <div className="App"> 25 26 <Router> 27 <div> 28 <a> <Link to={'/'} >Index</Link></a> 29 <a> <Link to={'/about'} >about</Link></a> 30 <a> <Link to={'/access'} >access</Link></a> 31 <Route exact={true} path={'/'} component={Index} /> 32 <Route path={'/about'} component={About} /> 33 34 // error箇所 35 <Route path={'/access'} component={ReduxAccessWithRouter} /> 36 </div> 37 </Router> 38 </div> 39 ); 40 } 41} 42 43export default App; 44

表題のとおりです。このコードの問題はReduxAccessWithRouterを作る部分の箇所で
react-routerのマニュアルに寄るとwithRouter(connect(...)(MyComponent)) という変換で目的のコンポーネントを作れるかと思ったんですが以下のようなエラーが出ます

sh

1./src/App.tsx 2(19,78): error TS2345: Argument of type '(props: RouteComponentProps<{}> & TestProps) => Element' is not assignable to parameter of type 'Component<DispatchProp<any> & TestProps>'. 3 Type '(props: RouteComponentProps<{}> & TestProps) => Element' is not assignable to type 'StatelessComponent<DispatchProp<any> & TestProps>'. 4 Types of parameters 'props' and 'props' are incompatible. 5 Type 'DispatchProp<any> & TestProps & { children?: ReactNode; }' is not assignable to type 'RouteComponentProps<{}> & TestProps'. 6 Type 'DispatchProp<any> & TestProps & { children?: ReactNode; }' is not assignable to type 'RouteComponentProps<{}>'. 7 Property 'match' is missing in type 'DispatchProp<any> & TestProps & { children?: ReactNode; }'.

AccessReduxStoreのpropsの定義の仕方が問題なのかと思いますが、どうしても上手く生きません。
ご教授お願い致します。

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

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

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

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

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

guest

回答1

0

自己解決

変更箇所のコードだけ纏めます

typescript

1interface TestProps extends RouteComponentProps<{}> { 2 state: Object; 3} 4const AccessReduxStore = (props: TestProps) => 5 (<h1>access to redux store{JSON.stringify(props.state)}</h1>); 6 7const mapStateToProps = (state: {}, ownProps: TestProps): TestProps => { 8 return { 9 state, 10 ...ownProps 11 }; 12}; 13 14const ReduxAccessWithRouter = withRouter(connect(mapStateToProps)(AccessReduxStore));

投稿2017/07/05 07:10

m0a

総合スコア708

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問