まずはコードをご確認下さい
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の定義の仕方が問題なのかと思いますが、どうしても上手く生きません。
ご教授お願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。