Javascript 到 Clojurescript 与 react-spring 互操作

Javascript to Clojurescript interop with react-spring

我正在尝试将 react-spring (https://www.react-spring.io/docs/hooks/basics) 实现到我的 Clojurescript 产品中,我正在努力将其转换为 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"]
))

变量propsreturns这个:

#js{:opacity #object[AnimatedValue [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]

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

我确实设法让一个基本示例起作用,希望它能帮助您入门:

  • 我克隆了我的示例存储库:https://github.com/dfuenzalida/cljs-react-intl/(我创建这个是为了在 react-intl 上进行测试,对你也有用)
  • 从签出的存储库中,我安装了 react-springyarn install 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 下载任何缺少的 dep,然后 yarn run htmlyarn shadow-cljs watch app 以监视模式启动编译器。控制台将显示如下内容:

$ 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 加载您的应用程序后,您应该会看到如下内容:

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

希望对您有所帮助!