首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clojurescript/试剂/chart.js-试剂生命周期- chart.update()

Clojurescript/试剂/chart.js-试剂生命周期- chart.update()
EN

Stack Overflow用户
提问于 2017-11-11 04:46:45
回答 1查看 550关注 0票数 3

我们将chart.js与clojurescript和Reagent结合使用。现在我知道Chart.js有一个chart.update()方法用来用新数据更新图表。所以问题是,我如何设置我的组件,使其呈现图表:试剂渲染,但可能获得:组件-将-更新调用chart.update()方法?

基本上,有没有一种方法可以获得图表的句柄,该图表是从:component-will-update函数中的试剂渲染函数创建的?

EN

回答 1

Stack Overflow用户

发布于 2018-08-01 05:25:27

在这种情况下,通常遵循的模式是将有状态/可变对象包装在React组件中,并使用React的生命周期方法。

re-frameUsing-Stateful-JS-Component中详细描述了这种方法,但这是我倾向于使用D3来实现的方式:

代码语言:javascript
复制
(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),但我自己从来没有试过。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47230863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档