简体   繁体   English

this.props.children不与react-router一起使用

[英]this.props.children isnt working with react-router

i am trying to make a navigaton on my website by react-router and typescript. 我试图通过react-router和Typescript在我的网站上进行导航。 but its not working and instead of Home page i get an empty page 但它不起作用,而不是主页我得到一个空白页

Thats my app.tsx file 那就是我的app.tsx文件

// app.tsx

import * as React from 'react';
import '../App.css';

class App extends React.Component<any,any> {
  public render() {
    return (
      <div>
          {this.props.children}
      </div>
    );
  }
}

export default App;

index.tsx index.tsx

// index.tsx

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import './index.css';
import registerServiceWorker from './registerServiceWorker';
import {AppRouter} from './router';


ReactDOM.render(
  <AppRouter/>,
  document.getElementById('root') as HTMLElement
);
registerServiceWorker();

router.tsx router.tsx

import * as React from 'react';
import { Route,Router } from 'react-router';

import App from './components/App';

import HomePage from './components/home/HomePage';

import createHistory from 'history/createBrowserHistory';

const history = createHistory();

export const AppRouter = () => {
    return (
        <Router history={history}>
            <Route path="/" component={App} >
                <Route path="/" exact={true} component={HomePage} />
            </Route>
        </Router>
    );
}

i dont really dont know what to say. 我真的不知道该说些什么。 i cant find any possible info about this. 我找不到有关此的任何可能信息。 If u know better way to make navigation with typescript, react and redux i am open for ideas. 如果您知道使用打字稿进行导航的更好方法,反应和还原,那么我会公开征求意见。 thx 谢谢

In the App component, you are doing {this.props.children}. 在App组件中,您正在执行{this.props.children}。 So you should not pass that component to any route. 因此,您不应将该组件传递给任何路由。

You need to add all the routes as a child of that component through which you will get the children in your props. 您需要将所有路线添加为该组件的子代,通过该子代可以将这些子代添加到道具中。

Also, for switching between routes, you should bind all the routes in the 'Switch' imported from react-router. 另外,要在路由之间进行切换,应将所有路由绑定到从react-router导入的“ Switch”中。

So, your router.tsx file should look like: 因此,您的router.tsx文件应如下所示:

import * as React from 'react';
import { Route, Router, Switch } from 'react-router';
import App from './components/App';
import OtherComponent from './components/OtherComponent';
import HomePage from './components/home/HomePage';
import createHistory from 'history/createBrowserHistory';
const history = createHistory();

export const AppRouter = () => {
  return (
    <App>
      <Router history={history}>
        <Switch>
          <Route exact={true} component={HomePage} />
          <Route path="/other" component={OtherComponent} />
        </Switch>
      </Router>
    </App>
  );
}

By this, you will get the 'HomePage' component at route '/' and OtherComponent at route '/other'. 这样,您将在路由“ /”处获得“ HomePage”组件,并在路由“ / other”处获得OtherComponent。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM