reactjs - 使用试剂中的预定义 react 成分?

标签 reactjs clojurescript om reagent

我有一些带有反应组件抽象的外部 UI,我想从试剂中重用它们,有没有什么方法可以通过从 clojurescript 传递数据来直接渲染预定义的 react 组件。我是 clojurescript 初学者。

最佳答案

我们来试试吧!我们可以从在 js 文件中编写组件开始。

var CommentBox = React.createClass({displayName: 'CommentBox',
  render: function() {
    return (
      React.createElement('div', {className: "commentBox"},
                          this.props.comment
      )
    );
  }
});

然后我们可以直接从Reagent中调用它:

(defonce app-state
  (atom {:text "Hello world!"
         :plain {:comment "and I can take props from the atom"}}))

(defn comment-box []
  js/CommentBox)

(defn hello-world []
  [:div
   [:h1 (:text @app-state)]
   [comment-box #js {:comment "I'm a plain React component"}]
   [comment-box (clj->js (:plain @app-state))]])

(reagent/render-component [hello-world]
                          (. js/document (getElementById "app")))

请注意,我们通过使用 #js 将 props 作为纯 js 对象传递给 CommentBox 并将原子转换为纯 js clj-> js。如果我错过了什么,你可以在gist中找到其余的内容。 .

关于reactjs - 使用试剂中的预定义 react 成分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28759108/

相关文章:

javascript - 为什么这个 javascript 函数返回 undefined ?

Clojure 嵌套 for 循环和索引

javascript - 如何使用 clojurescript 或 om-next 在 dom 元素上切换类

css - om 中的动态样式表

javascript - 测试 react 加载组件

javascript - Jest 匹配器匹配三个值中的任何一个

reactjs - 在react js和react native expo之间共享代码

javascript - ClojureScript-Lib 和我在同一页上的 ClojureScript

clojure - 使用 lein 将依赖项添加到 clojure 项目

twitter-bootstrap-3 - 在 Om 中使用 bootstrap 下拉菜单