2017-10-04 23 views
0

リア・ルータv2からv4にアプリを移行しています。リアクションルータv2をv4のonEnter引数にマイグレーション

古いv2のコードは次のようになります。

<Route path="/configurations/:id" onEnter={loadConfiguration}> 
    // some stuff 
</Route> 

新しいv4のコードは次のようになります - nextState

<Route path="/configurations/:id" component={MyComponent}> 
    // some stuff 
</Route> 

export default class MyComponent extends Component { 
    componentDidMount() { 
    loadConfiguration() 
    } 
    // render method, etc 
} 

問題はloadConfiguration機能v2の中3つのparamsと呼ばれているということです置換、コールバック

v4コードでは、loadConfiguration関数は0個のパラメータで呼び出されます。

v2からv4への移行時にloadConfigurationに同じ値を渡すにはどうすればよいですか?

<Route path="/configurations/:id" render={ 
() => <MyComponent nextState={ nextState } replace={ replace } callback={ callback } /> 
} /> 

そして、あなたのコンポーネントで:

export default class MyComponent extends Component { 

    loadConfiguration() { 
    ... 
    } 

    componentDidMount() { 
    this.loadConfiguration(this.props.example, this.props.replace, this.props.callback) 
    } 

    // render method, etc 

} 

ます。また、親からloadConfiguration機能をダウン渡すことができ

答えて

1

あなたのコンポーネントに小道具を渡すをレンダリング使用することができます必要に応じて小道具として使用し、コンポーネント内でthis.props.loadConfiguration()と呼んでください。


注:

<Route path="/configurations/:id" render={ 
    (props) => <MyComponent example={ props.example } /> 
} /> 
:あなたはまた、親から小道具を渡すことができます
関連する問題