繁体   English   中英

Javascript 到 Clojurescript 与 react-spring 的互操作

[英]Javascript to Clojurescript interop with react-spring

我正在尝试将react-springhttps://www.react-spring.io/docs/hooks/basics )实现到我的 Clojurescript proct 中,我正在努力将其转换为clojurescript

import {useSpring, animated} from 'react-spring'
function App() {
  const props = useSpring({opacity: 1, 
                           from: {opacity: 0}})
  return <animated.div style={props}>I will fade in</animated.div>
}

到目前为止,这就是我所做的:我需要以下内容:

(:require 
[useSpring]
[animated])

在 let 块中我有这样的东西:

(defn example-app []
   (let [props (useSpring (->js {:opacity 1
                                 :from  {opacity: 0}}))]
    (def props props)
    [:animated.div {:style props} "I will fade in"]
))

变量props返回:

#js{:opacity #object[AnimatedValue [object Object]]}

这就是我渲染动画 object 的方式

(react-dom/render
(hx/f [example-app])
(goog.dom/getElement "example-app"))

这是我得到的错误

#object[Error Invariant Violation: Objects are not valid as a React child (found: object with keys {ns, name, fqn, _hash, cljs$lang$protocol_mask$partition0$, cljs$lang$protocol_mask$partition1$}). If you meant to render a collection of children, use an array instead.
in core$example_app]

我究竟做错了什么? 我错过了什么?

我确实设法获得了一个基本示例,希望它可以帮助您入门:

  • 我克隆了我的示例 repo: https://github.com/dfuenzalida/cljs-react-intl/ (我创建这个是为了在 react-intl 上进行测试,对你也有用)
  • 从签出的仓库中,我使用yarn install react-spring react-spring
  • 我编辑了文件shadow-cljs.edn以添加对lilactown/hx的依赖:
{:source-paths ["src"]
 :dependencies [[lilactown/hx "0.5.3"]]
 :dev-http {8080 "target/"}
 :builds {:app {:output-dir "target/"
                :asset-path "."
                :target :browser
                :modules {:main {:init-fn app.main/main!}}
                :devtools {:after-load app.main/reload!}}}}

我用 react-spring 示例替换了src/app/main.cljs的内容,稍作调整:

(ns app.main
  (:require [hx.react :as hx :refer [defnc]]
            ["react-dom" :as react-dom]
            ["react-spring" :as spring]))

(defnc AppComponent [{:keys [title]}]
  (let [props (spring/useSpring (clj->js {:opacity 1 :from {:opacity 0}}))]
    [:<>
     [spring/animated.div {:style props} title]]))

;; App initialization

(defn mount-root []
  (react-dom/render
   (hx/f [AppComponent {:title "I will fade in"}])
   (js/document.getElementById "app")))

(defn main! []
  (mount-root)
  (println "[core]: loading"))

(defn reload! []
  (mount-root)
  (println "[core] reloaded"))

最后,运行yarn install下载任何缺失的 deps,然后yarn run htmlyarn shadow-cljs watch app以 watch 模式启动编译器。 控制台将显示如下内容:

$ yarn shadow-cljs watch app
yarn run v1.17.3
$ /tmp/cljs-react-intl/node_modules/.bin/shadow-cljs watch app
shadow-cljs - config: /tmp/cljs-react-intl/shadow-cljs.edn  cli version: 2.8.74  node: v10.16.0
shadow-cljs - updating dependencies
...
shadow-cljs - dependencies updated
shadow-cljs - HTTP server available at http://localhost:8080
shadow-cljs - server version: 2.8.74 running at http://localhost:9630
shadow-cljs - nREPL server started on port 43685
shadow-cljs - watching build :app
[:app] Configuring build.
[:app] Compiling ...
[:app] Build completed. (156 files, 11 compiled, 0 warnings, 9.21s)

http://localhost:8080加载应用程序后,您应该会看到如下内容:

代码和浏览器截图

animation 确实有效,但我不知道如何使其他示例工作(例如滚动示例或计数器)。

我希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM