为什么我的组件不显示使用反应?

时间:2017-07-22 02:54:31

标签: reactjs react-native react-router react-redux

我正在尝试显示react-component。我添加了一些路由mechanism。但它没有显示我的组件。 这是我的代码

https://codesandbox.io/s/WnJl4DKOJ

import React from 'react';
import {Route} from 'react-router-dom';
import Hello from '../Hello';

const Routers = ()=> (
    <Route exact path="/" component={Hello}></Route>
)

export default Routers;

Index.js

import React from 'react';
import { render } from 'react-dom';
import {Router } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
import Routers from './router/router';

const history = createHistory();


render(
      <Router routes={Routers} history={history}></Router>

  , document.getElementById('root'));

2 个答案:

答案 0 :(得分:1)

您未正确使用<Router />。让<Routers />成为<Router />的孩子。

在您的示例中,将<Router routes={Routers} history={history}></Router>更改为:

<Router history={history}>
    <Routers />
</Router>

答案 1 :(得分:0)

<Router/>提供路线所需的上下文。

您需要将代码修改为

import React from 'react';
import {Route} from 'react-router-dom';
import Hello from '../Hello';

const Routers = ()=> (
    <Router>
        <Route exact path="/" component={Hello}/>
    </Router>
)

export default Routers;

<强> index.js

import React from 'react';
import { render } from 'react-dom';
import {Router } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';
import Routers from './router/router';

const history = createHistory();


render(<Routers history={history}/>, document.getElementById('root'));