如何使用Provider,Router和Route设置项目以使用React,Redux和TypeScript进行路由?

时间:2017-05-30 22:06:41

标签: typescript react-router react-redux react-router-redux

我正在使用TypeScript,React和Redux开发一个项目。目前,我遇到了一个问题,我想将未经过身份验证的用户直接路由到登录页面,否则将路由到内容页面。

我的主要问题是我收到了TypeScript编译错误,因为我无法找出Route组件的正确类型定义。我在网上检查了几个教程,而我无法设法移植他们的代码并让它与我的例子一起工作。

这是我的index.tsx:

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { Router, Route } from 'react-router';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, combineReducers } from 'redux';
import { routerReducer, routerMiddleware } from 'react-router-redux';
import { Reducers, InitialState } from './reducers';

import thunk from 'redux-thunk';
import createHistory from 'history/createBrowserHistory';

import App from './app/App';
import Content from './components/pages/Content';
import Login from './components/pages/Login';
import Overview from './components/containers/Overview';
import Payment from './components/containers/Payment';
import Profile from './components/containers/Profile';

const history = createHistory();    
const middleware = routerMiddleware(history);

const store = createStore(
  combineReducers({
    ...Reducers,
    router: routerReducer
  }),
  InitialState,
  applyMiddleware(thunk.withExtraArgument(middleware)),
);

function isAuth() {
  let user = FirebaseApp.auth().currentUser;
  return user !== null;
}

const app = document.getElementById('root') as HTMLElement;
ReactDOM.render(
  <Provider store={store}>
    <Router history={history}>
      <Route component={App}>
        <Route path="/" component={Content}>
          <Route path="/" exact={true} component={Overview} />
          <Route path="/payment" component={Payment} />
          <Route path="/profile" component={Profile} />
        </Route>
        <Route onEnter={isAuth}>
          <Route component={Login} />
        </Route>
      </Route>
    </Router>
  </Provider>,
  app
);

IDE在以下行显示错误:IDE shows error 并且编译器抛出以下错误:     编译失败。

./src/index.tsx
(60,14): error TS2322: Type '{ component: ComponentClass<{}>; children: Element[]; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Route> & Readonly<{ children?: ReactNode; }> & Rea...'.
  Type '{ component: ComponentClass<{}>; children: Element[]; }' is not assignable to type 'Readonly<RouteProps>'.
    Types of property 'component' are incompatible.
      Type 'ComponentClass<{}>' is not assignable to type 'StatelessComponent<RouteComponentProps<any> | undefined> | ComponentClass<RouteComponentProps<any...'.
        Type 'ComponentClass<{}>' is not assignable to type 'ComponentClass<RouteComponentProps<any> | undefined>'.

另请参阅应用程序,内容和其中一个页面(例如个人资料)定义:

// App:
import * as React from 'react';
import './App.css';
import { connect } from 'react-redux';
import { GlobalState } from '../reducers';
export class App extends React.Component<any, any> {
  render() {
    return (
      <div id="viewport" className="App" />
    );
  }
}
function select(state: GlobalState) {
  return state.sessionState;
}
export default connect(select)(App);

// Content:
import * as React from 'react';
import { RouteComponentProps } from 'react-router';
import Header from '../header/Header';
import Footer from '../footer/Footer';
export default class Content extends 
React.Component<RouteComponentProps<{}>, void> {
    render() {
        return (
            <div>
                <Header />
                <div className="Layout" id="container" />
                <Footer />
            </div>
        );
    }
}

// Login:
import * as React from 'react';
import { RouteComponentProps } from 'react-router';
export default class Login extends React.Component<RouteComponentProps<{}>, void> {
    render() {
        return (
            <div>
                <input type="email" value="" />
                <input type="password" value="" />
                <input type="submit" value="Login" />
            </div>
        );
    }
}

// Profile:
import * as React from 'react';
import { RouteComponentProps } from 'react-router';
export default class Profile extends React.Component<RouteComponentProps<{}>, void> {
    render() {
        return (
            <div>
                Profile
            </div>
        );
    }
}

我尝试将我的实现基于以下GitHub存储库 dictionary-react-redux-typescript

1 个答案:

答案 0 :(得分:2)

我认为这与我提出问题的问题相同。 Here it is。我相信它已修复,所以尝试更新你的“@ types / react-router”包。