clojurescript原子的哪些变化导致试剂组分重新渲染? [英] Which changes to clojurescript atoms cause reagent components to re-render?

查看:142
本文介绍了clojurescript原子的哪些变化导致试剂组分重新渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下试剂组分。它使用ref函数,它根据span元素的实际大小更新局部状态原子。这是为了重新呈现其自身大小的组件。

Consider the following reagent component. It uses a ref function, which updates a local state atom, based on the real size of a span element. This is done in order to re-render the component displaying its own size

(defn show-my-size-comp []
  (let [size (r/atom nil)]
    (fn []
      (.log js/console "log!")
      [:div
       [:span {:ref (fn [el]
                      (when el (reset! size (get-real-size el))))} 
        "Hello, my size is:" ]
       [:span (prn-str @size)]])))

c $ c> get-real-size 返回向量,日志消息不断打印,这意味着组件不必要地被重新渲染。如果它只返回一个数字或字符串,日志只出现两次 - 在这种情况下的意图。

If the implementation of get-real-size returns a vector, the log message is printed constantly, meaning the component unnecessarily being re-rendered all the time. If it returns just a number or a string, the log appears only twice - as intended in this scenario.

这是什么原因?是否可能用一个新的向量(包含相同的值)更新clojure脚本原子内部意味着放置另一个JavaScript对象,从而改变原子?而放一个值不会产生可观察到的变化?只是猜测... *

What's the reason for this? Is it maybe that updating an clojure script atom with a new vector (containing the same values though) internally means putting another JavaScript object there, thus changing the atom? Whereas putting a value produces no observable change? Just speculation...*

无论如何 - 对于真正的用例,保存跨度的尺寸在向量中肯定会更好..有没有办法实现这一点?

Anyways - for the real use case, saving the size of the span in a vector would certainly better.. Are there ways to achieve this?

当我尝试改善这个问题。

I cam across this, when trying to enhance the answer given in this question.

*自JS:({} === {})// false

推荐答案

您可以使用 not = check:

You can work around the problem with a not= check:

                (fn [el]
                  (when el
                    (let [s (get-real-size el)]
                      (when (not= s @size)
                        (reset! size s)))))

我不知道为什么向量应该与其他值不同的原因。

I'm not sure what the reason is for why vectors should differ from other values.

这篇关于clojurescript原子的哪些变化导致试剂组分重新渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆