GithubHelp home page GithubHelp logo

Comments (3)

mike-thompson-day8 avatar mike-thompson-day8 commented on May 4, 2024

Normally, you'd ask this question in the clojurescript google group. It isn't reagent specific.

Before discussing which of the two is better, note that there is a third variation: don't wrap the render-component in a function. Just have this code at the bottom of your core.cljs:

(reagent/render-component [todo-app] (.-body js/document))

Now, there's no longer any need to call run. When the javascript is loaded, the render-component will execute.

So now to answer your question about calling the kickoff function: most of the time this is a matter of personal taste, but there is one case where you MUST use an onload variation. That's when you are using :optimizations :none (in the cljsbuild section, within project.clj).

In that case, the HTML would need to include a goog.require:

<script type="text/javascript" src="target/client.js"></script>
<script type="text/javascript">
  goog.require('todomvc");      ;;  <---- triggers a cascade of  <script> to be added to the page
  todomvc.run();                 ;;  fails because the code is not loaded yet  (<scripts>  are still loading async)
</script>

To work around this failure, you have to wait for the whole page to load before you call run(). Perhaps you cheat like this:

<script type="text/javascript" src="target/client.js"></script>
<script type="text/javascript">
  goog.require('todomvc");      ;;  <---- triggers a cascade of  <script> to be added to the page
  function run()  {
      todomvc.run();  
  }
  window.addEventListener('load', run);      ;; you can't reference todomvc.run directly, because the code isn't laoded.
</script>

Or use the approach in chord where the onload is burried in the code.

Personally, I like to see the kickoff function called in the HTML.

from reagent.

Frozenlock avatar Frozenlock commented on May 4, 2024

I consider using directly

(reagent/render-component [todo-app] (.-body js/document))

to be quite dangerous, unless you know for sure that you'll always render to the body.

I myself like to render into inner DIVs, which means that I must call the rendering function after the DIVs themselves are loaded. This is most easily done with the onload function.

Using todomvc.run(); has the advantage of giving you more control. Because of the way cljs works, you might end up with a single js file for multiple webpage. By calling the functions individually, you can decide which ones are going to be executed on a given page.

In summary:

  • Single page app: use onload;
  • Multiple webpage: use todomvc.run() for precise control;

from reagent.

stig avatar stig commented on May 4, 2024

Thank you very much, both. @mike-thompson-day8 You're right; I should have asked this on a ClojureScript forum instead. I've not seen this approach anywhere else though.

from reagent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.