5

I would like to use this React.js component as a foreign Reagent component in a ClojureScript application :

https://github.com/felixrieseberg/React-Spreadsheet-Component.

This component is however not available in the repository:

http://cljsjs.github.io/.

If a React.js component is available in the directory then using it with Reagent would be as simple as in the following example.

(ns demo.views
  (:require [reagent.core :as reagent]
            [cljsjs.reactable]])

(defn example []
  [:div
  [:> Reactable.Table
    {:data (clj->js [
                 {:name "Foo" :section 51}
                 {:name "Bar" :section 51}
                 ])}
    ]
   ]
  )

I would like to know what I would have to do with the React Spreadsheet Component such that I can use it in a similar simple way. How to prepare a React.js component for usage in ClojureScript as an external Reagent component? Please provide a clear recipe type of description.

Note: This question How to use a ReactJS Component with Clojurescipt/Reagent looks similar but does not answer my question.

2 Answers 2

3

It boils down to how you want to do JavaScript interop... you have 3 choices:

  1. Include the js from your HTML file
  2. Build it as part of your compilation
  3. Include it as a library

I encourage you to try (3); it isn't difficult, just follow the steps on CLJSJS https://github.com/cljsjs/packages/wiki/Creating-Packages

Sign up to request clarification or add additional context in comments.

3 Comments

Are you saying that with all three options I can use it as a an external Reagent component? I thought that was only possible with option 3. As for option 3, I looked at that description, of course, I find step 4 a really difficult one.
For step 4 you can use a generator like dotnetwise.com/Code/Externs/index.html so you don't need to do externs by hand.
Now that really helps!
0

Use ClojureScript's compiler options to include the external JS in the build, then use reagent's adapt-react-class to use the component in your reagent views. Try not to depend on projects like CLJSJS.

Doing this yourself will keep you in control of your project.

in project.clj

:foreign-libs [{:file "https://rawgit.com/felixrieseberg/React-Spreadsheet-Component/master/dist/spreadsheet.js"
                :provides  ["no-idea"]}]

in the views

(def reactable-table (r/adapt-react-class js/Reactable.Table))

(defn example []
  [:div
  [reactable-table
    {:data (clj->js [
                 {:name "Foo" :section 51}
                 {:name "Bar" :section 51}])}]])

Note however that this component bundles lots of dependencies (jQuery). You might be better of by making a component like this yourself, in clojurescript/reagent.

1 Comment

It comes to down to making an :externs file right? The point is that I haven't got a clue how to create such a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.