Baris ini hilang path
:
<Route name="ideas" handler={CreateIdeaView} />
Seharusnya:
<Route name="ideas" path="/:testvalue" handler={CreateIdeaView} />
Diberikan yang berikut Link
(usang v1) :
<Link to="ideas" params={{ testvalue: "hello" }}>Create Idea</Link>
Terbaru sejak v4 :
const backUrl = '/some/other/value'
<Link to={{pathname: `/${this.props.testvalue}`, query: {backUrl}}} />
dan di dalam withRouter(CreateIdeaView)
komponen render()
:
console.log(this.props.match.params.testvalue, this.props.location.query.backurl)
hello /some/other/value
Dari link yang Anda posting di dokumen, di bagian bawah halaman:
Diberikan rute seperti <Route name="user" path="/users/:userId"/>
Contoh kode yang diperbarui dengan beberapa contoh kueri yang dipotong:
const {
BrowserRouter,
Switch,
Route,
Link,
NavLink
} = ReactRouterDOM;
class World extends React.Component {
constructor(props) {
super(props);
console.dir(props);
this.state = {
fromIdeas: props.match.params.WORLD || 'unknown'
}
}
render() {
const { match, location} = this.props;
return (
<React.Fragment>
<h2>{this.state.fromIdeas}</h2>
<span>thing:
{location.query
&& location.query.thing}
</span><br/>
<span>another1:
{location.query
&& location.query.another1
|| 'none for 2 or 3'}
</span>
</React.Fragment>
);
}
}
class Ideas extends React.Component {
constructor(props) {
super(props);
console.dir(props);
this.state = {
fromAppItem: props.location.item,
fromAppId: props.location.id,
nextPage: 'world1',
showWorld2: false
}
}
render() {
return (
<React.Fragment>
<li>item: {this.state.fromAppItem.okay}</li>
<li>id: {this.state.fromAppId}</li>
<li>
<Link
to={{
pathname: `/hello/${this.state.nextPage}`,
query:{thing: 'asdf', another1: 'stuff'}
}}>
Home 1
</Link>
</li>
<li>
<button
onClick={() => this.setState({
nextPage: 'world2',
showWorld2: true})}>
switch 2
</button>
</li>
{this.state.showWorld2
&&
<li>
<Link
to={{
pathname: `/hello/${this.state.nextPage}`,
query:{thing: 'fdsa'}}} >
Home 2
</Link>
</li>
}
<NavLink to="/hello">Home 3</NavLink>
</React.Fragment>
);
}
}
class App extends React.Component {
render() {
return (
<React.Fragment>
<Link to={{
pathname:'/ideas/:id',
id: 222,
item: {
okay: 123
}}}>Ideas</Link>
<Switch>
<Route exact path='/ideas/:id/' component={Ideas}/>
<Route path='/hello/:WORLD?/:thing?' component={World}/>
</Switch>
</React.Fragment>
);
}
}
ReactDOM.render((
<BrowserRouter>
<App />
</BrowserRouter>
), document.getElementById('ideas'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router-dom/4.3.1/react-router-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/4.3.1/react-router.min.js"></script>
<div id="ideas"></div>
pembaruan:
Lihat: https://github.com/ReactTraining/react-router/blob/0c6d51cd6639aff8a84b11d89e27887b3558ed8a/upgrade-guides/v2.0.0.md#link-to-onenter-and-isactive-use-location-descriptors
Dari panduan peningkatan dari 1.x ke 2.x:
<Link to>
, onEnter, dan isActive menggunakan deskriptor lokasi
<Link to>
sekarang dapat menggunakan deskriptor lokasi selain string. Query and state props tidak digunakan lagi.
// v1.0.x
<Link to="/foo" query={{ the: 'query' }}/>
// v2.0.0
<Link to={{ pathname: '/foo', query: { the: 'query' } }}/>
// Masih valid di 2.x
<Link to="/foo"/>
Demikian juga, pengalihan dari hook onEnter sekarang juga menggunakan deskriptor lokasi.
// v1.0.x
(nextState, replaceState) => replaceState(null, '/foo')
(nextState, replaceState) => replaceState(null, '/foo', { the: 'query' })
// v2.0.0
(nextState, replace) => replace('/foo')
(nextState, replace) => replace({ pathname: '/foo', query: { the: 'query' } })
Untuk komponen seperti tautan khusus, hal yang sama berlaku untuk router.isActive, sebelumnya history.isActive.
// v1.0.x
history.isActive(pathname, query, indexOnly)
// v2.0.0
router.isActive({ pathname, query }, indexOnly)
pembaruan untuk v3 ke v4:
"dokumentasi migrasi warisan" untuk anak cucu
/ideas/${this.props.testvalue}
}> {this.props.testvalue} </Link>