我们将chart.js与clojurescript和Reagent结合使用。现在我知道Chart.js有一个chart.update()方法用来用新数据更新图表。所以问题是,我如何设置我的组件,使其呈现图表:试剂渲染,但可能获得:组件-将-更新调用chart.update()方法?
基本上,有没有一种方法可以获得图表的句柄,该图表是从:component-will-update函数中的试剂渲染函数创建的?
发布于 2018-08-01 05:25:27
在这种情况下,通常遵循的模式是将有状态/可变对象包装在React组件中,并使用React的生命周期方法。
在re-frame的Using-Stateful-JS-Component中详细描述了这种方法,但这是我倾向于使用D3来实现的方式:
(defn graph-render [graph-attrs graph-data]
;; return hiccup for the graph here
(let [width (:width graph-attrs)
height (:height graph-attrs)]
[:svg {:width width :height height}
[:g.graph]]))
(defn graph-component [graph-attrs graph-data]
(r/create-class
{:display-name "graph"
:reagent-render graph-render
:component-did-update (fn [this]
(let [[_ graph-attrs graph-data] (r/argv this)]
(update! graph-attrs graph-data)))
:component-did-mount (fn [this]
(let [[_ graph-attrs graph-data] (r/argv this)]
(init! graph-attrs graph-data)))}))
(defn container []
[:div {:id "graph-container"}
[graph-component
@graph-attrs-ratom
@graph-data-ratom]])保持外部/内部组合很重要,因为React不会正确地填充它的道具。
另一件要小心的事情是reagent/argv向量的返回,正如您所看到的,它包含了第一项之后的属性。我在上面的维基页面上见过(reagent/props comp),但我自己从来没有试过。
https://stackoverflow.com/questions/47230863
复制相似问题