1

我试图找出在 Reagent with Hiccup 中如何使一个元素占用所有可用空间。所以我会得到一个调整大小的父级:component-did-mount 回调。

(defn chart [id col-width row-height]
  (let [dimensions (atom {})]
    (reagent/create-class
      {:component-did-mount
       (fn [e]
         (let [thisComponent (aget (js/document.querySelector ".app") "parentNode")
               width (aget thisComponent "offsetWidth")
               height (aget thisComponent "offsetHeight")]
           (swap! dimensions {:width width :height height})
           (println "----did mountwdth" width "--" height col-width row-height)
           (.log js/console thisComponent)))
       :reagent-render
       (fn [id col-width row-height]
         [:div
          [:div {:style {:background "gray"}} "--drag handle--"]
          [:div.non-dragable
           [simple-bar id]
           [tchart id col-width (int (- row-height controls-height))]]])})))

我希望图表元素占用所有可用空间。

4

2 回答 2

1

React 生命周期回调就像ComponentDidMount不响应组件大小的变化。

如果您想在组件大小更改时触发回调 - 您需要使用一些第三方 React 库,例如react-measurereact-sizeme

另一种策略是在窗口调整大小时添加一个事件侦听器,并从那里获取组件的父大小。

于 2018-02-13T10:27:43.817 回答
1

为此,我使用了 React Virtualized 的AutoSizer。与 Reagent 集成的示例:

(ns example
  (:require
    [cljsjs.react]
    [cljsjs.react-virtualized]
    [goog.object :as gobject]
    [reagent.core :as r]))

(defn autosizer-example
  []
  (r/with-let [width (r/atom 500)
               _ (js/setTimeout #(reset! width 1000)
                                1000)]
    [:div {:style {:width (str @width "px")}}
     [:> js/ReactVirtualized.AutoSizer
      {:disableHeight true
       :disableWidth true}
      (fn [props]
        (let [width (gobject/get props "width")]
          (r/as-element
           [:div
            "Width of parent: " width])))]]))

文档:https ://github.com/bvaughn/react-virtualized/blob/master/docs/AutoSizer.md

于 2018-02-13T12:09:18.970 回答