3
我试图在我的网页中嵌入一个CodeMirror来编辑几个代码片段,一次一个。ClojureScript:如何更改CodeMirror与试剂反应
为此I:
- 有一个试剂原子
node-defs-atom
包含地图上的代码片段。 - 有另一个原子
node-history-atom
其中包含正在查看的片段的键 - 将CodeMirror的值设置为密钥中的代码映射的值。
这里是行不通:
(defn editor [node-defs-atom node-history-atom]
(reagent/create-class
{:reagent-render (fn [] (do [:textarea
{ :value (@node-defs-atom (last @node-history-atom))
:auto-complete "off"}]))
:component-did-mount (editor-did-mount node-defs-atom node-history-atom)
}))
(defn editor-did-mount [node-defs-atom node-history-atom]
(fn [this]
(let [codemirror (.fromTextArea js/CodeMirror
(reagent/dom-node this)
#js {:mode "clojure"
:lineNumbers true})]
......)))
更改node-history-atom
与reset!
没有做任何事情在CodeMirror文本。我真的不确定发生了什么问题。
如果有人可以告诉我应该在哪里提及(@node-defs-atom (last @node-history-atom))
,我会非常感激。
提前致谢!
非常感谢;我已经设法实现了这个功能,但是每次我想要改变文本时,我都必须写一个对.setValue的调用,而不是基于node-history-atom的值进行反应。是否有可能以这种方式发生? – Boyentenbi
每次你想分配一个新的值时,你必须调用.setValue,因为CodeMirror是有状态的,并且你不想每次我想它都创建它。但是可以通过'(reagent.ratom/run!(.setValue @ text-atom))'宏轻松完成分配。 – SerCe