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'));
도움을 주셔서 감사합니다
당신은 당신의 것을 Route
a <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'));
.NET의 API 변경 사항입니다 react-router
4.x
. 권장되는 접근 방식은 Route
s를 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
'IT Share you' 카테고리의 다른 글
선형 RGB 공간과 비선형 RGB 공간에서 색상 작업을 할 때 실질적인 차이점은 무엇입니까? (0) | 2020.11.09 |
---|---|
Android M 권한 대화 상자가 표시되지 않음 (0) | 2020.11.09 |
UITapGestureRecognizer를 사용할 때 탭한 자식보기 찾기 (0) | 2020.11.09 |
소수 N이 주어지면 다음 소수를 계산할까요? (0) | 2020.11.09 |
부트 스트랩 3 : navbar에서 드롭 다운 링크의 헤드를 클릭 할 수있게 만드는 방법 (0) | 2020.11.09 |