2017-04-13 2 views
7

nextリアクタのバージョンを使用していますが、パラメータが破棄されているようです。私は、以下のリダイレクトがchannelIdの値を保持することを期待していますが、toルートはリテラル文字列 ":channelId"をパスで代わりに使用しています。リダイレクトルータリダイレクトパラメータを落とす

<Switch> 
    <Route exact path="/" component={Landing} /> 
    <Route path="/channels/:channelId/modes/:modeId" component={Window} /> 
    <Redirect 
    from="/channels/:channelId" 
    to="/channels/:channelId/modes/window" /> 
</Switch> 

これはresolved issueのように見えますが、それは働いていません。 toルートに渡す必要があるものはありますか?

+0

を使用すると、ソリューション、マットを見つけましたか? –

+0

@SebastianRoth残念ながら、私は決してしなかった。私は、コンポーネント自体の中でリダイレクトを使っていろいろなことを今やっています。しかし、上にリンクされたスレッドで広告されているように動作するはずだから、これを検証するのは素晴らしいことです。 –

+1

FWIW、私はこの質問を反応ルータのDiscordチャンネルで尋ねました。私は同様のことをやってしまいました。ルートを使って、小道具から値を引き出したリダイレクトを返すレンダリングメソッドを持っていました。 –

答えて

4

私は、中にはそのようなロジックは、ルータ4源を反応させるのが見つかりませんでしたので、自分の回避策を記述します。

import pathToRegexp from 'path-to-regexp'; 
import { Route, Switch, Redirect } from 'react-router-dom'; 

const RedirectWithParams = ({ exact, from, push, to }) => { 
    const pathTo = pathToRegexp.compile(to); 
    return (
    <Route exact={exact} path={from} component={({ match: { params } }) => (
     <Redirect to={pathTo(params)} push={push} /> 
    )} /> 
    ); 
}; 

使用例:

<Switch> 
    <RedirectWithParams 
     exact from={'/resuorce/:id/section'} 
     to={'/otherResuorce/:id/section'} 
    /> 
</Switch> 
+0

あなたのコードとほとんど同じです:https://github.com/ReactTraining/react-router/blob/master/packages/react-router/modules/generatePath.js – coblr

+0

ありがとう、これはまさに私が探していたものです – Anil

2

は、ここで私は、他の回答に似て使用してきたものです依存関係はありません。

0

<Switch> 
    <Route exact path="/" component={Landing} /> 
    <Route path="/channels/:channelId/modes/:modeId" component={Window} /> 
    <Route 
    exact 
    path="/channels/:channelId" 
    render={({ match }) => (
     <Redirect to={`/channels/${match.params.channelId}/modes/window`} /> 
    )} 
    /> 
</Switch> 
0

私はこれをしなかった、それが働いた:

<switch> 
 
    <Route path={`/anypath/:id`} component={Anycomponent} /> 
 
    <Route 
 
     exact 
 
     path="/requestedpath/:id" 
 
     render={({ match }) => { 
 
     if (!Auth.loggedIn()) { 
 
      return <Redirect to={`/signin`} />; 
 
     } else { 
 
      return <Redirect to={`/anypath/${match.params.id}`} />; 
 
     } 
 
     }} 
 
    /> 
 
</switch>