2015-12-09 14 views
9

私はreact-router 1.0とその履歴の統合を使用しています。this.history.pushState()経由で状態を渡す

import { createHistory, useBasename } from 'history' 

const history = useBasename(createHistory)({ 
    basename: '/base-path' 
}); 

export default (
    <Router history={history}> 
     <Route path="/" component={require("Template")}> 
      <Route path="sub-page" component={require("SubPage")}/> 
     </Route> 
    </Router> 
); 

私たちのAPIからとURLにIDを削除し、自己リンクはJSONオブジェクトにHATEOASによって作成合格しようとしています。 私はpushState状態パラメータを介してデータを渡したいと思います。

onClickEditButton(selfLinkObject) { 
    this.history.pushState(selfLinkObject, "/my/url"); 
} 

しかし、状態を取得しようとすると、渡されません。

import React from "react" 
import { History } from "react-router" 

export default React.createClass({ 
    mixins: [History], 
    componentDidMount() { 
     console.log(this.state); 
     console.log(this.history.state); 
     console.log(history.state); 
    }, 
    render() { 
     <p>Check the console to see if the state is passed from the pushState(state, url, params);</p> 
    } 
}); 

ユーザーがブラウザを再起動した後、彼らが復元できるように、ブラウザがユーザーのディスクに状態オブジェクトが保存されますので、pushStateをMozilla's pushState documentationに応じて動作するように意図された方法であると思われます。

どのようにデータを渡すことができますか、それとももっと良い方法があるのでしょうか? this.props.location.stateのか、コンテキストのlocationオブジェクトを取得する:状態はとてもlocationである

答えて

28

、あなたはそれがあなたのルートコンポーネントの場合props経由でアクセスすることができます。

v3ドキュメントでPinterest exampleをチェックしてください。これはまさにあなたが望むことをしています。

編集:ここでは が反応ルータV4の例へのリンクです: https://reacttraining.com/react-router/web/example/modal-gallery

+7

DUDE !!!これは私が必要とするものです...ドキュメントのあらゆる部分を検索したと思います。本当にありがとう!あなたはロックスターです! –

+0

私は助けることができてうれしいです。これがドキュメントで明確でない場合は、推奨変更をPRに送ってください。 – knowbody

+0

リンクは存在しません。 –

関連する問題