0

我正在尝试将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]]}

这就是我渲染动画对象的方式

(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]

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

4

1 回答 1

0

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

  • 我克隆了我的示例 repo: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以下载任何缺少的 deps,然后在监视模式下启动编译器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加载应用程序后,您应该会看到如下内容:

代码和浏览器截图

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

我希望这有帮助!

于 2019-11-23T05:01:53.597 回答