web-dev-qa-db-ja.com

react-router v4でReact IndexRouteを使用する

オンラインチュートリアルで自分でReactを学んでいます。

したがって、これはReactルーターの使用に関する基本的な例です。

<Router history={browserHistory}>
  <Route path="/" component={App}>
    <IndexRoute component={Home}/>
    <Route path="/home" component={Home}/>
    <Route path="/about" component={About}/>
    <Route path="/contact" component={Contact}/>
  </Route>
</Router>

私のアプリコンポーネント:

class App extends React.Component {
   render() {
      return (
         <div>
            <ul>
              <li><Link to="home">Home</Link></li>
              <li><Link to="about">About</Link></li>
              <li><Link to="contact">Contact</Link></li>
           </ul>
          {this.props.children}
        </div>
     )
   }
}
export default App;

ただし、IndexRouteを使用すると問題が発生するため、npmでreact-router-dom v4のモジュールを検索すると、内部にIndexRouteがありません。代わりにこれを使用します:

<Router>
  <div>
  <ul>
    <li><Link to="/">Home</Link></li>
    <li><Link to="/about">About</Link></li>
    <li><Link to="/contact">Contact</Link></li>
  </ul>
  <hr/>
  <Route exact path="/" component={Home}/>
  <Route path="/about" component={About}/>
  <Route path="/contact" component={Contact}/>
  </div>
</Router>

それで、1つのパスに2つのコンポーネントをレンダリングするにはどうすればよいですか?

44
Quoc-Hao Tran

UPDATEreact-router-4は、子がなくなったという点で変更されました。ただし、Routeコンポーネントを使用すると、パスに一致するものなら何でもレンダリングできます。

<Router>
  <div>
    <ul>
      <li><Link to="/">Home</Link></li>
      <li><Link to="/about">About</Link></li>
      <li><Link to="/contact">Contact</Link></li>
    </ul>
    <hr/>

    // All 3 components below would be rendered when in a homepage
    <Route exact path="/" component={Home}/>
    <Route exact path="/" component={About}/>
    <Route exact path="/" component={Contact}/>

    <Route path="/about" component={About}/>
    <Route path="/contact" component={Contact}/>
  </div>
</Router>

試してみるには、このビンを参照してください https://www.webpackbin.com/bins/-Kf5cpLcax4dttAEvkCW

つまり、ラッパーが必要な場合は、コンポーネント内にラッパーを記述できます。

65

反応ルーター&IndexRouteなし

<Route exact path="/" component={Home}/>

===

<IndexRoute component={Home}/>

https://github.com/ReactTraining/react-router/issues/4732#issuecomment-31723922

# Switch


```jsx

<Route path="/about" component={About}/>
<Route path="/:user" component={User}/>
<Route component={NoMatch}/>

import { Switch, Route } from 'react-router'

<Switch>
    <Route exact path="/" component={Home}/>
    <Route path="/about" component={About}/>
    <Route path="/:user" component={User}/>
    <Route component={NoMatch}/>
</Switch>


/* there will only ever be one child here */

<Fade>
    <Switch>
        <Route/>
        <Route/>
    </Switch>
</Fade>

<Fade>
    <Route/>
    <Route/>
</Fade>

/* there will always be two children here, one might render null though, making transitions a bit more cumbersome to work out */

```

https://reacttraining.com/react-router/web/api/Switch
14
xgqfrms

これが役立つかどうかはわかりませんが、これらは私が使用したコードです。

ファイル構造:

src

-index.js

-app(フォルダー)

--components(フォルダー)

--- Header.js

--- Home.js

--- Root.js

--- User.js

src/app/index.js

import React, {Component} from  "react";
import { render } from "react-dom";
import { browserHistory} from "react-router";
import { BrowserRouter as Router, Route, IndexRoute} from "react-router-dom";

import Root from "./components/Root";
import Home from "./components/Home";
import User from "./components/User";

class App extends Component {
  render() {
    return (
        <Router history={browserHistory}>
            <div>
                <Root>
                    <Route exact path={"/"} component={Home} />    
                    <Route path={"/user"} component={User} />    
                    <Route path={"/home"} component={Home} />
                </Root>
            </div>
        </Router>
    )
  }
}

render (<App />, window.document.getElementById("app"));

src/app/components/Root.js

import React, {Component} from "react";
import { render } from "react-dom";

import Header from "./Header";
import Home from "./Home";

class Root extends Component{

    render(){
        let renderData;
        renderData = (
            this.props.children
        );

        return(
            <div>
                <div className="container">
                    <div className="row">
                        <div className="col-xs-10 col-xs-offset-1">
                            <Header/>
                        </div>
                    </div>
                    <div className="row">
                        <div className="col-xs-10 col-xs-offset-1">
                            {renderData}
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default Root;

src/app/components/Home.js

import React, {Component} from "react";

class Home extends Component{
    render(){
        return(
            <div>
                <p>{this.props.isExist}</p>
                <h2>Home</h2>
            </div>
        );
    }
}

export default Home;

src/app/components/User.js

import React, {Component} from "react";

class User extends Component{
    render(){
        return(
            <div>
                <h3>The user page</h3>
                <p>User ID:</p>
            </div>
        );
    }
}

export default User;

webpack.config.js

var webpack = require("webpack");
var path = require("path");

var DIST_DIR = path.resolve(__dirname, "dist");
var SRC_DIR = path.resolve(__dirname, "src");

var config = {
    entry: SRC_DIR + "/app/index.js",
    output: {
        path: DIST_DIR + "/app",
        filename: "bundle.js",
        publicPath: "/app/"
    },
    module:{
        loaders: [
            {
                test: /\.js?/,
                include: SRC_DIR,
                loader: "babel-loader",
                query:{
                    presets: ["react", "es2015", "stage-2"]
                }
            }
        ]
    }
};

module.exports = config;
2
    <Router history={browserHistory}>
        <div>
            <Root>
                <Redirect from="*" to="/home"/>
                <Route path="/home" component={Home}/>    
                <Route path={"/user"} component={User} />    
                <Route path={"/home"} component={Home} />
            </Root>
        </div>
    </Router>  try Please try this....
1
Bruno Souza

シンプルなソリューション

方法1:

<Route exact path="/" component={Home}/>


Note:-

<Route exact path="/" component={Home}/> 
and  <IndexRoute component={Home}/>
 both can comapre as same*

方法2:

npm install react-router@3 --save
0
ngCourse