如何在带有打嗝的试剂中制作一个具有所有可用空间的元素

时间:2018-02-13 08:54:49

标签: clojure clojurescript reagent hiccup

我试图找出带有Hiccup的Reagent如何制作一个带有所有可用空间的元素。所以我将得到一个调整大小的父级: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))]]])})))

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

2 个答案:

答案 0 :(得分:1)

ComponentDidMount这样的生命周期回调不会对组件大小的变化做出反应。

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

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

答案 1 :(得分:1)

我使用React Virtualized' s 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