【问题标题】:How to make two Component connect each other on React-Redux如何在 React-Redux 上使两个组件相互连接
【发布时间】:2016-10-13 16:56:53
【问题描述】:

我在 rails 项目中使用过 react/redux。所以我希望我的列表组件是伪装者(服务器端渲染),而其他组件只在鼠标悬停在列表项上时显示细节。

鼠标悬停事件

我的问题是当鼠标悬停在每个列表项上时,如何获取详细组件上的列表数据

简单示例

My Code on rails view
= react_component('Listing', { data: @listings }, prerender: true )
= react_component('Detail', { }, prerender: false )

My Code on JS
export default class Listings extends Component {
  render() {
    return (
      <Provider store={store}>
        <ListingsWidget />
      </Provider>
    );
  }
}

My Code for Detail

export default class ListingDetail extends Component {
  render() {
    return (
      <Provider store={store}>
        < ListingDetail Widget />
      </Provider>
    );
  }
}

【问题讨论】:

    标签: javascript ruby-on-rails reactjs redux react-redux


    【解决方案1】:

    您有一些伪代码,但您将有 3 个组件:ListingsListingsItemListingsItemDetail。您将在 ListingsItem 中的一个元素上拥有一个 React onMouseOver 属性,该属性将调用您的事件处理程序来设置状态。假设您的ListingsItemDetail 组件在ListingsItem 内,您将检查状态以查看是否应该显示ListingsItemDetail。如果ListingsItemDetail 在其他地方,那么您将调用作为道具传入的事件处理程序,或者使用 Redux 或其他东西来设置应该显示的ListingsItemDetail 的 id。

    编辑 - 添加部分示例:

    const ListItem = React.createClass({
        getInitialState() {
            return {showDescription: false}
        },
    
        handleMouseOver() {
            this.setState({showDescription: true})
        },
    
        handleMouseOut() {
            this.setState({showDescription: false})
        },
    
        renderDescription() {
            if (this.state.showDescription) {
                return (
                    <ListItemDescription description={this.props.item.description} />
                )
            }
        },
    
        render() {
            return (
                <div onMouseOver={this.handleMouseOver} onMouseOut={this.handleMouseOut}>
                    List item title: {this.props.item.title}
                    {this.renderDescription}
                </div>
            )
        }
    })
    

    【讨论】:

    • 你能举个简单的例子吗?
    猜你喜欢
    • 1970-01-01
    • 2019-09-05
    • 2018-02-28
    • 2020-10-20
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多