React网格元素,单击后显示有关指定元素的详细信息

时间:2017-12-08 19:47:49

标签: javascript reactjs jsx

我已经创建了一系列元素(类似于图库)AllElements我正在映射的组件SingleElement组件

renderAllElements = () => (
            this.state.myData.map(se => (
                <SingleElement key={se.id} name={se.name} tagline={se.tagline} image_url={se.image_url}/>
        )
    )
)

我的SingleElement呈现了这个,如下所示

render() {
    return (
        <div className="singleElement">
            <img src={this.props.image_url} alt={this.props.name} />
            <h4>{this.props.name}</h4>
            <h5>{this.props.tagline}</h5>
        </div>
    )
}

到了什么地步,我想要实现什么? ---&GT;点击其中一个元素(specyfied SingleElement)后,细节显示在屏幕前方(悬停在整个网格上)。让我们将此组件SingleElementDetails命名为。实现它的最佳方法是什么? SingleElementDetails组件应该是SingleElement组件或其子组的兄弟吗?

1 个答案:

答案 0 :(得分:1)

您可以使用AllElements状态和方法来处理何时/要显示的内容。 像这样:

class AllElements extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      myData: {},
      viewingElement: null,
    };
    this.see = this.see.bind(this);
    this.close = this.close.bind(this);
  }

  see(id) {
    this.setState({
      viewingElement: id,
    });
  }

  close() {
    this.setState({
      viewingElement: null,
    });
  }

  render() {
    const { myData, viewingElement } = this.state;
    return (
      <div>
        {myData.map(se => (
          <SingleElement
            key={se.id}
            name={se.name}
            tagline={se.tagline}
            image_url={se.image_url}
            see={this.see}
            close={this.close}
          />
        ))}
        {viewingElement && (
          <SingleElementDetails element={myData[this.state]} />
        )}
      </div>
    );
  }
}

然后,您需要从this.props.see点击on SingleElement上的SingleElementDetails并使用CSS在比赛的其余部分上直观地定位// Notice that IDisposable is not implemented here! public interface HttpClientHandle { HttpRequestHeaders DefaultRequestHeaders { get; } Uri BaseAddress { get; set; } // ... // All the other methods from peeking at HttpClient } public class HttpClientHander : HttpClient, HttpClientHandle, IDisposable { public static ConditionalWeakTable<Uri, HttpClientHander> _httpClientsPool; public static HashSet<Uri> _uris; static HttpClientHander() { _httpClientsPool = new ConditionalWeakTable<Uri, HttpClientHander>(); _uris = new HashSet<Uri>(); SetupGlobalPoolFinalizer(); } private DateTime _delayFinalization = DateTime.MinValue; private bool _isDisposed = false; public static HttpClientHandle GetHttpClientHandle(Uri baseUrl) { HttpClientHander httpClient = _httpClientsPool.GetOrCreateValue(baseUrl); _uris.Add(baseUrl); httpClient._delayFinalization = DateTime.MinValue; httpClient.BaseAddress = baseUrl; return httpClient; } void IDisposable.Dispose() { _isDisposed = true; GC.SuppressFinalize(this); base.Dispose(); } ~HttpClientHander() { if (_delayFinalization == DateTime.MinValue) _delayFinalization = DateTime.UtcNow; if (DateTime.UtcNow.Subtract(_delayFinalization) < base.Timeout) GC.ReRegisterForFinalize(this); } private static void SetupGlobalPoolFinalizer() { AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => { FinalizeGlobalPool(); }; } private static void FinalizeGlobalPool() { foreach (var key in _uris) { HttpClientHander value = null; if (_httpClientsPool.TryGetValue(key, out value)) try { value.Dispose(); } catch { } } _uris.Clear(); _httpClientsPool = null; } }