Giter Site home page Giter Site logo

Support seq as component about reagent HOT 2 CLOSED

robinheghan avatar robinheghan commented on May 8, 2024
Support seq as component

from reagent.

Comments (2)

holmsand avatar holmsand commented on May 8, 2024

Good point; I would like to allow any return value from component functions, but I'm not sure about the best way to implement that.

React requires that the render method returns a single component, which is what for example [:div "foo"] converts into. That means that a component function returning something that is not a single component (like a seq, that is converted into a javascript array, a string or a number) would have to be wrapped in some other component to keep React happy.

Wrapping string return values in a [:span ...] would probably be the right thing to do, but what about a seq? Should it be wrapped in a :div or a :span? Should a seq of length one be replaced with its first element, if that happens to be a component? Should a seq that happens to start with a keyword, as in your example, be converted into a vector, and thus into a single component (and, in that case, should this be allowed elsewhere as well)?

I'm also a little bit reluctant to create a solution that might become incompatible with React's way of solving the same "problem", if/when React allows other return values.

But there might be an incredibly elegant solution, that I just haven't though of…

from reagent.

robinheghan avatar robinheghan commented on May 8, 2024

Oh, i don't want to be able to return any value. The way it is now is logical and makes perfect sense. However, as several clojure functions return seqs, treating seqs and vecs equally would be a good thing. If a seq or vec starts with a keyword, treat it as a component, otherwise throw an error.

I guess, if you wanted to potentially support any return value, you could create a protocol, wrap-component or something, and then people could extend it as needed.

(defprotocol wrap-component
    (wrap [self]))

(extend-protocol wrap-component
    String
    (wrap [self] [:span self])
    Seq
    (wrap [self] (vec self]))

What do you think?

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.