This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-29
Channels
- # announcements (4)
- # babashka (38)
- # beginners (66)
- # calva (35)
- # cider (3)
- # clj-on-windows (19)
- # cljs-dev (2)
- # clojure (29)
- # clojure-europe (15)
- # clojure-norway (2)
- # clojurescript (43)
- # clr (7)
- # community-development (6)
- # events (3)
- # hoplon (5)
- # lsp (23)
- # malli (7)
- # matrix (9)
- # nbb (53)
- # off-topic (5)
- # overtone (1)
- # practicalli (2)
- # re-frame (6)
- # reagent (17)
- # reitit (11)
- # specter (5)
- # tools-build (7)
- # xtdb (22)
Hello! I'm writing a re-frame app and trying to use a JS datepicker component inside it, but I've been really struggling to make it work. The source is here: https://github.com/goderich/hr I would like to change the html date inputs for this: https://github.com/Hacker0x01/react-datepicker/ (or flatpickr, either one is fine). I'm vaguely aware that I would need to use form-3 components (as seen [here](https://github.com/day8/re-frame/blob/master/docs/Using-Stateful-JS-Components.md) and [here](https://github.com/reagent-project/reagent/blob/master/doc/CreatingReagentComponents.md)). However, I can't even get the DatePicker component to show up in the browser!
For future reference - #C0620C0C8 would be a better channel for this. There's also #C073DKH9P but it uses Reagent for rendering and that's exactly what you're having problems with. But what exactly have you tried so far? What is the code that attempts to render that component?
I'm calling the react-datepicker lib in views.cljs as follows (this part is not on GitHub cause it don't work):
["react-datepicker" :as react-datepicker :refer [DatePicker]]
I then try to create a function for it like this:
(defn datepicker-inner []
(let [date (js/Date.)]
(reagent/create-class
{:display-name "date picker"
:component-did-mount
(fn [comp]
(println "date picker did mount"))
:reagent-render
(fn []
[:div
[DatePicker
{:selected date
:onChange (fn [] (println "changed!"))}]])
})))
But the browser is complaining that "DatePicker" is not a function.I'm just trying to make the date picker show up at all. I figure once that is done, I can figure out the rest from there.
Two things to do:
1. Log the value of DatePicker
and confirm that it looks like a React component to exclude any possibility of an improper import
2. Instead of [DatePicker ...]
use [:> DatePicker ...]
- it's required to let React components work with Reagent hiccup.
By "log the value" I mean use (js/console.log ...)
since it will show the object's internals in the JS console of your browser.
Regular printing won't do here.
I'm seeing this error: Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
Hmmm. How should I fix the import? I'm at a bit of a loss here, sorry. Does the lib need to be in shadow-cljs.edn?
Log the value of react-datepicker
and send a screenshot of the result with the top level expanded.
Apologies for the stupid question, but what exactly do you mean by logging the value of react-datepicker?
Right after the (ns ...)
form, add (js/console.log 'xxx react-datepicker)
.
Then open the browser's DevTools, navigate to Console, and load the web page in the main window.
In the console, you should see a record that starts with xxx
and is followed by some value.
That value should be an expandable object - expand the top level, make a screenshot, and attach it here.
First of all, that xxx
is displayed as an object in your console and not as a CLJS symbol.
That means that you should install https://github.com/binaryage/cljs-devtools/ - it will make all CLJS value that are logged to the console be properly displayed.
The second object has a default
key - most likely it's a so-called default export from the react-datepicker
library.
And I'm gonna go out on a limb and guess that that's DatePicker
itself.
So what you wanna do then is require the library as ["react-datepicker$default" :as DatePicker]
.
This section of the docs describes all the important bits of how one might require NPM packages: https://shadow-cljs.github.io/docs/UsersGuide.html#_using_npm_packages
> banging my head against the wall for days Heh, yeah, don't do that unless you always have some lead to pursue. When you're fully stuck for more than 20-30 minutes, just ask someone.
The library assumes that the users will import the css directly in their JS source: https://github.com/Hacker0x01/react-datepicker/ We can't do that in CLJS can we? From what I understand, the only way is to import it in index.html
> When you're fully stuck for more than 20-30 minutes, just ask someone. Yeah I should be more open to asking for help. Self-reliance is great, but sometimes it bites you in the ass. This was one of those times XD My complete ignorance of JS, React, and Reagent isn't helping.
> We can't do that in CLJS can we?
We can't indeed.
Including it directly in HTML is one option, but you'll have to make that file available to your web server. I'd probably just copy it into your resources folder with a build hook and then add it to .gitignore
.
I myself use SASS, so I simply require all such files in my main .scss
and then it gets built and loaded in the main HTML.
Or rather, we can, but that would require using webpack and organizing your whole app around that.
Well, there is a server still - it's just on GitHub's premises. And the statement still stands true - you gotta make that CSS file available to the server somehow, which is probably just putting it into the right dir.
BTW if it's just a small app then you might find https://github.com/day8/re-com/ sufficient. Then you won't have to deal with NPM packages, at least not directly. And the library does have a date picker component.