I'm building a website with ClojureScript, reagent, re-frame, and bootstrap. I'm trying…
- User
I'm building a website with ClojureScript, reagent, re-frame, and bootstrap. I'm trying to get a dynamically updating barcode by implementing JSBarcode. Here is my code:
(ns ui.new (:require ["reactstrap" :as rs] ["jsbarcode" :as barcode] [finance.core.calculation :refer [force-rounding]] [formatters :as formatters] [utils.core :refer [throw-exception]] [cljs.pprint :refer [cl-format]] [clojure.string :as string] [goog.dom :as gdom] [goog.style :as gstyle] [reagent.core :as ra] [reagent.dom :as rdom] ["react-dom" :refer (createPortal)]))
(defn barcode-component [value barcode-id & _] (fn [this] (let [node (rdom/dom-node this)]) (barcode (.getElementById js/document barcode-id) (if (seq value) value 0) {:format "CODE128" :lineColor "#000000" :width 2 :height 40 :displayValue true})))
(defn barcode-render [value] (let [barcode-id (str "barcode-" (random-uuid))] (ra/create-class {:component-did-mount (barcode-component value barcode-id) :component-did-update (fn [this old-argv] (let [old-value (second old-argv)] (when (not= old-value value) (barcode-component value barcode-id this)))) :reagent-render (fn [] [:svg {:id barcode-id}])})))
I'm calling it like so in another file:
(defn- scancode-section [] [:div [ui/row row-config [label-col {:class "col-md-3 col-sm-6 col-6 justify-content-end"} "Scanning Code"] [input-col {:class "col-md-3 col-sm-6 col-6 justify-content-end"} [text-input-field :scan-code]]] [ui/col {:class "col-md-3 col-sm-6 col-6"} [:p (str @(rf/subscribe [:inventory-edit/get-form-field :scan-code]))] ; debug to check if the field is getting updates properly [new/barcode-render @(rf/subscribe [:inventory-edit/get-form-field :scan-code])]]])
The values seem to be updated as per the commented line, but the rendered barcode itself is not dynamically updating.
- Assistant
- User
It's still not updating automatically as the code is entered. However, it seems to be updated for a split second when the page hot-reloads when I'm saving changes to the code.
- Assistant
- User
Uncaught TypeError: svg_element.innerHTML is not a function
- Assistant
- User
Uncaught TypeError: svg_element.firstChild is not a function
- Assistant
- User
When I look at the log, I see "Updating barcode for: " but no value. Is it getting passed in properly?
- Assistant