clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
kari 2021-01-24T09:08:04.065500Z

If someone is interested in how to use the Vega-Lite (https://vega.github.io/vega-lite/) graphics library with Clojurescript I created an exercise about it: https://www.karimarttila.fi/clojure/2021/01/19/world-statistics-exercise.html

2đź‘Ť
Neros 2021-01-24T09:14:04.065800Z

I am trying to get a QR code scanner to work in my project, I have tried several different ways but I am still getting problems. The most basic QR code example in react I have found is the following: I am trying to get a QR code scanner to work in my project, I have tried several different ways but I am still getting problems. The most basic QR code example in react I have found is the following:

import React from 'react';
import BarcodeScannerComponent from "react-webcam-barcode-scanner";

function App() {

  const [ data, setData ] = React.useState('Not Found');

  return (
    <>
      <BarcodeScannerComponent
        width={500}
        height={500}
        onUpdate={(err, result) => {
          if (result) setData(result.text)
          else setData('Not Found')
        }}
      />
      <p>{data}</p>
    </>
  )
}

export default App;
Any suggestions would be most grateful, thanks.

oliver 2021-01-24T10:58:57.067500Z

@info527: maybe the wrong channel? Otherwise, if you are doing this with reagent or somesuch in ClojureScript, then maybe post a CLJS snippet and the problems you encounter.

Neros 2021-01-24T11:43:37.067900Z

Thanks, yea I will post the cljs in a moment

clyfe 2021-01-24T14:33:38.069200Z

Help! NPM integration issue (details in thread): > No such namespace: @codemirror/state

clyfe 2021-01-24T14:36:29.069300Z

clyfe 2021-01-24T14:37:31.070300Z

Note: done npm install and installed ok.

mkvlr 2021-01-24T15:22:52.070500Z

are you trying clojure-mode?

mkvlr 2021-01-24T15:23:14.070700Z

I think you’re running into https://clojure.atlassian.net/browse/CLJS-3293

mkvlr 2021-01-24T15:25:02.071100Z

if you’re using bundle you’ll need the first one

mkvlr 2021-01-24T15:26:04.071300Z

try replacing your clojurescript dep with

{:git/url "<https://github.com/nextjournal/clojurescript>"
 :sha "f48609b862062caae6d19d5d281c4f7b53a39c25"}

mkvlr 2021-01-24T15:30:08.071500Z

alternatively you can use https://github.com/nextjournal/clojure-mode-demo/ as a skeleton which works with shadow and unpatched ClojureScript

1đź‘Ť
clyfe 2021-01-24T15:44:50.071700Z

thank you!

mkvlr 2021-01-24T15:47:25.071900Z

you’re welcome, let me know if it helps

1đź‘Ť
mkvlr 2021-01-24T15:47:40.072100Z

if you have any questions about clojure-mode, feel free to post in #nextjournal

clyfe 2021-01-24T16:03:48.072300Z

cm demo works, git/sha doesn't for some reason will continue from the demo

mkvlr 2021-01-24T16:04:44.072700Z

what’s the error?

clyfe 2021-01-24T16:17:20.072900Z

Same No such namespace: @codemirror/state

mkvlr 2021-01-24T16:18:40.073100Z

hmm

Neros 2021-01-24T21:04:54.075500Z

@oliver this is how I had converted the above react code to work in shadow-cljs:

(ns app.product.views.qr-scanner
  (:require
   [reagent.core :as r]
   [reagent.dom :as rd]
   ["react-webcam-barcode-scanner" :refer [BarcodeScannerComponent]]))

(defn qr-scanner []
  (let [[data setData] (.useState js/React "Not Found")]
    [:&gt; BarcodeScannerComponent
     {:height 500
      :width 500
      :onUpdate (fn [err result]
                  (if result
                    (setData (.-text result)) (setData "Not Found")))}
     (fn [result]
       (r/as-element [:p result]))]))
I am getting errors in the console saying, reagent.impl.component.js:219 Error rendering component (in app.product.views.qr_scanner.qr_scanner) app.product.views.qr_scanner.js:8 Uncaught ReferenceError: React is not defined

thheller 2021-01-24T21:29:47.076200Z

@info527 add ["react" :as react] to your ns require and use (react/useState ...) instead of js/React

Neros 2021-01-24T21:30:13.076500Z

thankyou, Ill try it now

Neros 2021-01-24T21:34:22.077200Z

@thheller thanks for the prompt reply, I am getting the following error now in the console: Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

thheller 2021-01-24T21:43:23.077800Z

yeah the useState hook you can only use in a function component. not sure how thats done in reagent. docs should have it. might be :f&gt; or so

Piotr Brzeziński 2021-01-24T22:25:41.080200Z

Hi. I’m playing with clojurescript + reagent for the first time and got a question. I want to have a timer that I can start by clicking on it and re-set on another click. I run the timer that updates my r/atom using setInerval but am not sure how/where to clear interval. I tried adding it conditionally at the end of my anonymous onclick FN but it fails saying fexpr.call is not a function

(defn counter-row [{desc :desc timer :timer active :active bg-color :bg-color}]
      (let [seconds-elapsed (r/atom 0)
            running (r/atom false)
            timer-fn (fn [] (js/setInterval #(swap! seconds-elapsed inc) 1000))]
           (fn []
               [:div {:class (str "h-full flex flex-col flex-1 " bg-color " bg-opacity-75 items-center justify-evenly")
                      :on-click (fn []
                                    #(reset! running (not @running))
                                    (timer-fn))}
                [:p {:class "w-full text-9xl text-center text-gray-50"} desc]
                [:p {:class "w-full text-9xl text-center text-gray-50"} @seconds-elapsed]])))
all help appreciated.

Piotr Brzeziński 2021-01-24T22:47:27.081100Z

By reset I mean set seconds-elapsed to 0 and stop counting

Piotr Brzeziński 2021-01-25T09:53:53.086800Z

Thanks! Let me try that out.

Piotr Brzeziński 2021-01-25T10:13:25.087Z

Following your advice, I changed the code to this -> https://gist.github.com/brzezinskip/73464bfe3b9298671c47f11024db37e1 so now setInterval is assigned to timer-fn atom, but for some reason when I call js/cleaInterval timer-fn it doesn’t do anything.

Piotr Brzeziński 2021-01-25T10:13:36.087200Z

The interval keeps counting :thinking_face:

p-himik 2021-01-25T10:14:12.087400Z

It should be (js/clearInterval @timer-fn).

Piotr Brzeziński 2021-01-25T10:14:18.087600Z

Ah yeah, just noticed that

Piotr Brzeziński 2021-01-25T10:14:26.087800Z

Darn, I have problems remembering that.

Piotr Brzeziński 2021-01-25T10:14:31.088Z

Thanks a lot, now it works like a charm 🙂

1đź‘Ť
p-himik 2021-01-24T23:54:02.081200Z

If @running then call clearInterval, otherwise call setInterval. Save the result of setInterval in an atom (you can use a regular atom instead of a ratom here) and pass it later to clearInterval. A small thing - consider using reagent.core/with-let instead of a form-2 component. It won't change how it works but will make some things simpler. Also, it will make it much easier to call clearInterval when the component is unmounted (you cannot do it at all with form-2).