IT Share you

React-Router는 자식 하나만

shareyou 2020. 11. 9. 21:36
반응형

React-Router는 자식 하나만


계속 오류가 발생합니다.

'라우터'에는 하나의 하위 요소 만있을 수 있습니다.

반응 라우터를 사용할 때.

예제에 표시된 코드와 똑같기 때문에 이것이 작동하지 않는 이유를 알아낼 수없는 것 같습니다. Quick Start

내 코드는 다음과 같습니다.

import React from 'react';
import Editorstore from './Editorstore';
import App from './components/editor/App';
import BaseLayer from './components/baselayer';
import {BrowserRouter as Router, Route} from 'react-router-dom';
import {render} from 'react-dom';

const root = document.createElement('div');
root.id = 'app';
document.body.appendChild(root);

const store = new Editorstore();
const stylelist = ['https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.2/semantic.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css', 'https://api.tiles.mapbox.com/mapbox-gl-js/v0.33.1/mapbox-gl.css'];

stylelist.map((link) => {
    const a = document.createElement('link');
    a.rel = 'stylesheet';
    a.href = link;
    document.body.appendChild(a);
    return null;
});

render((
  <Router>
    <Route exact  path="/" component={BaseLayer} />
    <Route path="/editor" component={App} store={store} />
  </Router>
), document.querySelector('#app'));

도움을 주셔서 감사합니다


당신은 당신의 것을 Routea <div>(또는 a <Switch>) 로 감싸 야합니다 .

render((
  <Router>
    <Route exact  path="/" component={BaseLayer} />
    <Route path="/editor" component={App} store={store} />
  </Router>
), document.querySelector('#app'));

해야한다

render((
  <Router>
    <div>
       <Route exact  path="/" component={BaseLayer} />
       <Route path="/editor" component={App} store={store} />
    </div>
  </Router>
), document.querySelector('#app'));

jsfiddle / webpackbin


.NET의 API 변경 사항입니다 react-router 4.x. 권장되는 접근 방식은 Routes를 https://github.com/ReactTraining/react-router/issues/4131#issuecomment-274171357 에 래핑 하는 것입니다 Switch.

인용 :

변하게 하다

<Router>
  <Route ...>
  <Route ...>
</Router>

...에

<Router>
  <Switch>
    <Route ...>
    <Route ...>
  </Switch>
</Router>

물론 Switch가져 오기에 다음 을 추가 해야합니다.

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

나는 항상 반응 웹과 네이티브에서 Fragment를 사용합니다 (> = react 16)

import React, { Component, Fragment } from 'react'
import { NativeRouter as Routes, Route, Link } from 'react-router-native'
import Navigation from './components/navigation'    
import HomeScreen from './screens/home'
import { RecipesScreen } from './screens/recipe'

class Main extends Component {
  render() {
    return (
      <Fragment>
        <Navigation />
        <Routes>
          <Fragment>
            <Route exact path="/" component={HomeScreen} />
            <Route path="/recipes" component={RecipesScreen} />
          </Fragment>
        </Routes>
      </Fragment>
    )
  }
}

export default Main

I put all my <Route /> tags inside the <Switch> </Switch> tag like this.

    <BrowserRouter>
        <Switch>
            <Route path='/' component={App} exact={true} /> 
            <Route path='/form-example' component={FormExample} />
        </Switch>
    </BrowserRouter>

This solves the problem.


If you are nesting other components inside the Router you should do like.

  <Router>
     <div>
       <otherComponent/>
         <div>
           <Route/>  
           <Route/>
           <Route/>
           <Route/>
         </div>
      </div>
    </Router>

you can also wrap all your route in a parent route which defaults to index page

<Router history={history}>    
   <Route path="/" component={IndexPage}>
      <Route path="to/page" component={MyPage}/>
      <Route path="to/page/:pathParam" component={MyPage}/>
   </Route>    
</Router>

I am using react-router-dom package with version 5.0.1 and it works perfectly fine with your code.

import { BrowserRouter as Router , Router, Link } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
...

class App extends React.Component {
  render() {
    return (
      <Router>
        <ul>
          <li><Link path='/'>Home</Link></li>
          <li><Link path='/about'>About</Link></li>
        </ul>
        <Route path='/' exact component={Home} />
        <Route path='/about' component={About} />
      </Router>
    );
  }
}

export default App;

This problem occurs when you don't have parent tag before <Route>inside <Router> so to resolve this problem keep the <Route>enclosed in a parent tag such as <div> , <p> etc. Example -

<Router>
    <p>
       <Route path="/login" component={Login} />
       <Route path="/register" component={Register} />
    </p>
</Router>

참고URL : https://stackoverflow.com/questions/42992911/react-router-only-one-child

반응형