Fork me on GitHub
#nbb
<
2022-08-12
>
Dimitar Uzunov15:08:37

Possibly a question for #beginners but here goes: idk what I’m doing wrong here:

(ns instances
  (:require ["ink" :refer [render Text]]
            ["ink-table" :refer [Table]]
            [reagent.core :as r]))
;; ...
(defn table-instances []
  [:> Table {:data table-data} @state])
Could not resolve symbol: Table
[5:46 PM] I’m literally doing the same thing as the example for the Text component
[5:47 PM]   (defn hello []
    [:> Text {:color "green"} "Hello, world! " @state])
this doesn’t throw an error

Dimitar Uzunov15:08:38

This is the example I wanted to do in cljs/nbb: https://github.com/maticzav/ink-table#usage

borkdude15:08:05

Can you do a console.log with Table ? It might be nil because of the $default you might have to use

Dimitar Uzunov15:08:52

"#error {:message \"Could not resolve symbol: Table\", :data {:type :sci/error, :line nil, :column nil, :file nil, :phase \"analysis\"}}"

borkdude15:08:11

hmm right. you can try ["ink-table$default" :refer [Table]]?

borkdude15:08:17

oh right: import Table from ... is similar to $default :as Table

lilactown15:08:56

Can you post the whole file?

Dimitar Uzunov15:08:19

(ns instances
  (:require ["ink" :refer [render Text]]
            ["ink-table$default" :refer [Table]]
            [reagent.core :as r]))

(js/console.log Table)

Dimitar Uzunov15:08:13

{
  "dependencies": {
    "ink": "3.2.0",
    "ink-table": "3.0.0",
    "nbb": "0.7.132"
  }
}

Dimitar Uzunov15:08:21

this is the package.json

borkdude15:08:20

so write: "...$default" :as Table]

Dimitar Uzunov15:08:48

yay that seems to have worked 🙂

Dimitar Uzunov15:08:52

{
  Header: [Function: Header],
  Cell: [Function: Cell],
  Skeleton: [Function: Skeleton],
  default: [class Table extends Component]
}
Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.
    at App (<path>/instances/node_modules/ink/build/components/App.js:42:9)

borkdude15:08:41

@ULE3UT8Q5 You might have to write :f>

borkdude15:08:57

hmm, no it seems Table is a React class

lilactown15:08:01

You only need :f> if it's a reagent component that uses hooks

borkdude15:08:34

or those that are written as function App () <pre>Hello</pre> right?

borkdude15:08:54

aka function components, I believe?

lilactown15:08:26

No, only reagent components that use hooks

lilactown15:08:52

Function components that don't need reagent (hiccup or reactions) can use :>

lilactown15:08:16

It's all very confusing

Dimitar Uzunov15:08:06

confusing indeed, wasn’t as simple as I thought 🙂

borkdude16:08:33

It is indeed confusing. This simple example works for me:

(ns script
  (:require ["ink" :refer [render Text]]
            ["ink-table$default" :as Table]
            [reagent.core :as r]))

(defn Basic []
  [:> Text "Hello"])

(render (r/as-element [Basic]))

borkdude16:08:56

But this doesn't:

(defn Basic []
  [:> Table {:data (clj->js [{:name "Name 1"}
                             {:name "Name 2"}])}])

(render (r/as-element [Basic]))

Dimitar Uzunov16:08:59

yeah I had made a mistake it is (render (r/as-element [table-instances])) not (render r/as-element [table-instances])

Dimitar Uzunov16:08:08

is a keys need to be unique error?

borkdude16:08:32

puling reagent out of the equation:

(render (React.createElement Table (clj->js {:data [{:name "Name 1"}
                                                    {:name "Name 2"}]})))

borkdude16:08:19

(this still doesn't work btw)

borkdude16:08:39

I get the same error for:

import { render } from "ink";
import { createElement } from "react";
import Table from "ink-table";


render(createElement(Table));

borkdude16:08:46

oh I got it, it's that typescript stuff again

borkdude16:08:17

This works:

(ns script
  (:require ["ink" :refer [render Text]]
            ["ink-table$default.default" :as Table]
            [reagent.core :as r]))

(render
 (r/as-element [:> Table {:data (clj->js [{:name "Name 1"}
                                          {:name "Name 2"}])}]))
cc @ULE3UT8Q5

borkdude16:08:59

In JS:

import { render } from "ink";
import { createElement } from "react";
import Table from "ink-table";

const Basic = () => {
      return createElement(Table.default, {data: [{name: "Hello"}]});
}

render(createElement(Basic));

Dimitar Uzunov16:08:19

it works for me too 🙂

Dimitar Uzunov16:08:05

so, how do you know to require it like this? ["ink-table$default.default" :as Table]

borkdude16:08:39

trial and error unfortunately. When you print Table you see that it has another default property

👍 1
borkdude16:08:47

This is usually the effect of some typescript transpilation

Dimitar Uzunov16:08:19

thanks, this is useful to know, I would have never thought to do default.default

alvinfrancis16:08:34

Hello everyone! Quick question. Is there any way to load clojure(script) libraries other than having the source in the file system?

alvinfrancis16:08:56

Oops never mind. Just saw the classpath workaround in the docs.

borkdude17:08:13

@U83GQU8LE You can specify deps in nbb.edn now as well

alvinfrancis17:08:39

Ooh checking out the PR and that looks much better indeed

borkdude17:08:58

Note that not all deps are compatible with nbb 🤞

alvinfrancis16:08:55

And on the flip side, is it possible to directly require js files (not from node_modules)?

borkdude17:08:19

Yes, you can if you do "./foo.js"

alvinfrancis17:08:20

Do you mean something like:

(ns dev
  (:require ["./foo.js" :refer [bar]]))

alvinfrancis17:08:25

Hmm getting an error if I try it:

alvinfrancis17:08:37

❯ npx nbb nrepl-server :port 40000
nREPL server started on port 40000 on host 127.0.0.1 - 
(node:30355) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

borkdude17:08:04

Yes, this is node behavior. you can rename your file to .mjs or put "type": "module" in your package.json

alvinfrancis17:08:40

Ahh that works! Thanks! I should read up on this.

borkdude17:08:56

The rule on Node.js is that you cannot load common JS modules (regular .js files) from ES6 modules and nbb is an ES6 module

borkdude17:08:11

No sorry this is wrong

borkdude17:08:38

You can load normal .js files but you can't use import in those

alvinfrancis17:08:31

Hm. I'm honestly not very well aware on the module stuff with node. But I'm assuming anything with import/export = CJS?

alvinfrancis17:08:15

no wait, common js is the one with require

👍 1
borkdude17:08:17

Anything with import / export / await = ES6 module and should end either in .mjs or the default should be set with type = module

alvinfrancis17:08:57

interesting. not something I've encountered before in JS code I've worked with. We use es6 but we neither have the type set to module or use mjs. I'm assuming this has something to do with bundling as to why neither is used in code that uses import/export/await.

alvinfrancis17:08:34

Too many things made easy but not simple in js land ^^;

alvinfrancis17:08:42

Ahh reading up on it and it's babel doing transpiling of the es modules

alvinfrancis17:08:58

At least in our case