2017-08-31 109 views
1

Ive得到能传递对象(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 
    } 
} 

我想要做的是在同一时间更改URL与推动/产品/ foo的,但我不能完全弄清楚,我把这段代码 - 任何帮助,通过调用赞赏

+0

内的动作定义一个有趣的内部类,并分配到的onClick,里面好玩首先调用那么行动改变路线。 –

+1

啊是的谢谢你有道理,你可能会发送一些示例代码即时通讯仍在挣扎 – rhysclay

+1

检查@ shubham的回答它将解决您的问题:) –

回答

1

你可以做到这一点FUNC重刑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

完美 - 非常感谢。我需要做的唯一额外事情是将post对象添加到selectPost方法中:selectPost =(post)=> {this.props.history.push('/ product/foo'); this.props.actions.selectPost(post); } – rhysclay

+0

我喜欢这种方式,因为我只是将数据从redux传递到下一个组件,所以加载是即时的,我甚至不需要URL参数的网络请求 - 因此/产品/富 – rhysclay

+0

很高兴它适合你 –