helix

https://github.com/Lokeh/helix
Christopher Stone 2020-11-01T16:04:58.091500Z

Hello, I am trying to use helix to display a leaflet map but am not able to get it to display... it would be handy if the docs showed a few different examples using use-state and use-effect :once to display the initial map... can somebody who knows how to use helix send me a working code example of how to use form 3 components, thanks peeps

lilactown 2020-11-01T16:13:11.091900Z

I’m not sure what you mean by a form 3 component?

lilactown 2020-11-01T16:13:58.093Z

If you know the JS you’d write, it’s not hard to do a mechanical translation into helix. I can help with that

Christopher Stone 2020-11-01T16:17:19.094100Z

var map = L.map('map', { center: [51.505, -0.09], zoom: 13 });

Christopher Stone 2020-11-01T16:19:10.095900Z

thats the code that displays the map, 'map' after the opening brackets represents the html element id it will me displayed in

Christopher Stone 2020-11-01T16:21:15.096900Z

I'll send the link to leaflets website it may make more sense to you: https://leafletjs.com/reference-1.7.1.html

lilactown 2020-11-01T16:24:26.097700Z

can you show me what you’ve tried so far?

Christopher Stone 2020-11-01T16:26:30.098Z

sure...

Christopher Stone 2020-11-01T16:26:36.098200Z

(defnc customer-map [] (use-effect :once (fn [] (L.map "map" (clj->js {:center default-center :zoom 9 :layers [(L.tileLayer osm-url copy-osm)] })))))

lilactown 2020-11-01T16:30:02.100600Z

The use-effect macro wraps everything you pass in after the dependencies (in this case :once) in a function for you

lilactown 2020-11-01T16:30:44.101800Z

So right now, the function you’re passing in is being returned by the function that use-effect creates to wrap it, and not running

lilactown 2020-11-01T16:31:28.102900Z

Well, it runs on unmount 😅

Christopher Stone 2020-11-01T16:31:45.103500Z

I don't understand how it all works...

Christopher Stone 2020-11-01T16:32:00.104200Z

Thanks for taking the time to help me by the way

1
lilactown 2020-11-01T16:32:17.104600Z

If you remove the function you’re wrapping (L.map ,,,) in it will get you closer

Christopher Stone 2020-11-01T16:33:01.105300Z

how will the map be displayed then?

lilactown 2020-11-01T16:33:49.105700Z

It will run when the effect gets run

Christopher Stone 2020-11-01T16:34:28.106Z

but I have that code nowhere else

Christopher Stone 2020-11-01T16:35:55.106900Z

here is an example I found written in js, using hooks;

Christopher Stone 2020-11-01T16:36:10.107100Z

import React from 'react';
import L from 'leaflet';

function Map() {
  React.useEffect(() => {
    // create map
    L.map('map', {
      center: [49.8419, 24.0315],
      zoom: 16,
      layers: [
        L.tileLayer('http://{s}.<http://tile.osm.org/{z}/{x}/{y}.png|tile.osm.org/{z}/{x}/{y}.png>', {
          attribution:
            '&amp;copy; &lt;a href="<http://osm.org/copyright>"&gt;OpenStreetMap&lt;/a&gt; contributors'
        }),
      ]
    });
  }, []);


  return &lt;div id="map"&gt;&lt;/div&gt;
}

export default Map;

Christopher Stone 2020-11-01T16:37:02.107800Z

how would I convert that to use helix?

lilactown 2020-11-01T16:38:03.108400Z

I mean write it like this:

(defnc customer-map []
  (use-effect
   :once
   (L.map "map" (clj-&gt;js {:center default-center
                          :zoom 9
                          :layers [(L.tileLayer osm-url copy-osm)]})))
  ,,,)

lilactown 2020-11-01T16:38:14.108700Z

without the (fn [] ,,,) wrapping the body of the effect

lilactown 2020-11-01T16:39:37.109300Z

the docs on hooks document the differences between React's useEffect and helix's use-effect: https://cljdoc.org/d/lilactown/helix/0.0.12/doc/hooks#doing-side-effects

lilactown 2020-11-01T16:41:17.110800Z

words are hard! and this is a confusing bit of syntactical difference between React and helix. helix's use-effect automatically wraps whatever you pass into it in a (fn [] ,,,), so wrapping it again, isn't doing what you want here

Christopher Stone 2020-11-01T16:42:24.112400Z

I tried it both ways, I am going to give it another go now...

lilactown 2020-11-01T16:43:05.112800Z

e.g.:

(react/useEffect
  function () {
    console.log("hi");
  }, [])
is the same as
(use-effect
 :once
 (js/console.log "hi"))

lilactown 2020-11-01T16:43:50.113300Z

there could be another error, but this way we'll definitely be running that effect on mount

Christopher Stone 2020-11-01T16:44:12.113600Z

this is the error I get

Christopher Stone 2020-11-01T16:44:36.113800Z

react-dom.development.js:23276 Uncaught #error {:message "Props received were … onent as a function."Props received were a map. This probably means you're calling your component as a function., :data {:props nil}} commitRootImpl @ react-dom.development.js:23276 exports.unstable_runWithPriority @ scheduler.development.js:647 runWithPriority$1 @ react-dom.development.js:11277 commitRoot @ react-dom.development.js:22991 performSyncWorkOnRoot @ react-dom.development.js:22330 scheduleUpdateOnFiber @ react-dom.development.js:21882 updateContainer @ react-dom.development.js:25483 (anonymous) @ react-dom.development.js:26022 unbatchedUpdates @ react-dom.development.js:22432 legacyRenderSubtreeIntoContainer @ react-dom.development.js:26021 exports.render @ react-dom.development.js:26104 reagent$dom$render_comp @ dom.cljs:18 (anonymous) @ dom.cljs:48 (anonymous) @ dom.cljs:38 app$core$start @ core.cljs:82 app$core$init @ core.cljs:94 (anonymous) @ shadow.module.main.append.js:2

Christopher Stone 2020-11-01T16:45:04.114200Z

Props received were a map. This probably means you're calling your component as a function.

lilactown 2020-11-01T16:45:23.114600Z

it sounds like you might be calling your component like (customer-map {:foo "bar"}) ?

Christopher Stone 2020-11-01T16:45:40.114800Z

I'm not

lilactown 2020-11-01T16:46:03.115100Z

okay. I see reagent in the stack trace

lilactown 2020-11-01T16:46:27.115600Z

how are you trying to render the helix component from reagent?

Christopher Stone 2020-11-01T16:47:40.116100Z

as a hicup component

Christopher Stone 2020-11-01T16:48:23.117Z

but I've tried it with and without square brackets and normal brackets

lilactown 2020-11-01T16:50:37.117800Z

I can help with that. Before that, though, is there a particular reason you're using reagent and helix together?

Christopher Stone 2020-11-01T16:53:26.118500Z

my app is using re-frame so I need reagent

lilactown 2020-11-01T16:55:11.119100Z

okay. when you want to render a helix component inside a reagent one, the easiest way to do it is to use helix's $ macro

lilactown 2020-11-01T16:56:14.120200Z

e.g.:

(defn app
  [:div
   [:h1 "My App!"]
   [menu options]
   [:div
    ($ customer-map)]])

Christopher Stone 2020-11-01T16:57:22.121Z

excellent, thank you I shall try that now, thank you so much 🙂

lilactown 2020-11-01T16:57:33.121300Z

sure thing! and here's a link to the docs on $: https://github.com/lilactown/helix/blob/master/docs/creating-elements.md

Christopher Stone 2020-11-01T17:23:41.121900Z

Thank you so much, I have it working now

Christopher Stone 2020-11-01T17:24:11.122400Z

Have a great day and keep up the good work

2