2017-08-31 12 views
1

アイブ氏は、アクションにオブジェクト(selectedPost)を渡し、単純なクリックハンドラを得た - 以下のコンポーネントである:ここではリアクト - Reduxのルータプッシュ

class SearchBarContainer extends Component { 
    render() { 
    return (
     <div> 
      <div className="center-xs"> 
      <p>To get started, type in some keywords below</p> 
      </div> 
      <SearchBar onTermChange={this.props.actions.loadPosts} onNull={this.props.actions.nullPosts}/> 
      <PostList posts={this.props.posts} isFetching={this.props.isFetching} onClick={this.props.actions.selectPost}/> 
     </div> 
    ); 
    } 
} 

はアクションです:

export function selectPost(post) { 
    return { 
    type: SELECT_POST, 
    payload: post 
    } 
} 

私は何をしたいが、同時に製品/ fooの/を押してURLを変更しているが、私はこのコードを置く場所はかなり把握カント - 任意のヘルプは

+0

は、クラス内の楽しさを定義し、onClickのに割り当て、その楽しさの内側に最初のルートを変更、その後のアクションを呼び出します。 –

+1

ああ、ありがとう、ありがとう、おそらくいくつかのサンプルコードを送信することができます私はまだ苦労 – rhysclay

+1

チェック@ shubhamの答えはあなたの問題を解決するでしょう:) –

答えて

1

に感謝あなたは呼び出すことによってそれを行うことができます機能ンonClickして、ルートを変更するだけでなく、関数内からアクションを呼び出す

class SearchBarContainer extends Component { 
    selectPost = (post) => { 
     this.props.history.push('/product/foo'); 
     this.props.actions.selectPost(post) 
    } 
    render() { 
    return (
     <div> 
      <div className="center-xs"> 
      <p>To get started, type in some keywords below</p> 
      </div> 
      <SearchBar onTermChange={this.props.actions.loadPosts} onNull={this.props.actions.nullPosts}/> 
      <PostList posts={this.props.posts} isFetching={this.props.isFetching} onClick={this.selectPost}/> 
     </div> 
    ); 
    } 
} 
export default withRouter(SearchBarContainer); 
+0

パーフェクト - ありがとうございます。私がする必要があった唯一の追加作業は、ポストオブジェクトをselectPostメソッドに追加することでした。selectPost =(post)=> { this.props.history.push( '/ product/foo'); this.props.actions.selectPost(post); } – rhysclay

+0

私はちょうど次のコンポーネントにreduxからデータを渡しているので、このアプローチが大好きです。読み込みは即時ですし、ネットワークリクエストのURLパラメータも必要ない - したがって/ product/foo – rhysclay

+0

うれしい –

関連する問題