-
-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sequences and om/react keys #40
Comments
I think adding :key to the attribute-map of is the correct way of Aaron Iba [email protected] writes:
Moritz Ulrich |
@the-kenny, I'm not sure adding :key attributes is the best solution here. Why should it be necessary in the above example but not when writing: (html [:table
[:tbody
[:tr [:th "last-name"] [:th "first-name"] [:th "date"]]]]) Nor is it necessary when writing: (html [:table
[:tbody
(into [:tr]
(for [k [:last-name :first-name :date]]
[:th (name k)]))]]) In all 3 ways of writing this, I don't think the :key attribute is relevant to React. |
@aiba in the first case you have something like React.DOM.tbody(null,
React.DOM.tr(null, [
React.DOM.th(null, "last-name"),
React.DOM.th(null, "last-name"),
React.DOM.th(null, "last-name")
]) (note, that In the second and third case: React.DOM.tbody(null,
React.DOM.tr(null,
React.DOM.th(null, "last-name"),
React.DOM.th(null, "last-name"),
React.DOM.th(null, "last-name")
)) In this case |
@mbme thank you for clarifying how the forms translate to React calls. That all makes sense. Sometimes it is desirable to pass sub-elements to the React.DOM function as an array, and sometimes it is desirable to pass as function arguments (see my example above). (Server-side hiccup does not have this distinction and treats e.g. Interestingly, on the sabolono main README file, the first example I think sablono ought to allow users to specify whether they want arguments or array. One way to accomplish this would be to provide a function How does that sound? |
Yeah, just was getting this error message as well and was wondering what the solution would be. As was mentioned, hiccup ignores the extra list generated by the |
I think unwrapping is the right way to go here. I'm also getting this error. |
You could try unquote-splice
|
I would love to see a fix for this, as it causes me to use (into) a whole bunch. Is this recognized as an issue? Should I submit a pull request? |
@timothypratley patch welcome! |
Anybody who got here and looking for a workaround: Change this: [:table {:class "table"}
[:thead
[:tr
[:th "origo"]
(for [w header] [:th w])]] To that: [:table {:class "table"}
[:thead
[:tr
[:th "origo"]
(for [w header] ^{:key w} [:th w])]] Kudos to Frozenlock on #clojurescript on freenode |
@l1x That does not work for me. Perhaps it is because I'm putting it inside a parent that has an option [:label
[:span "Filters"]
[:select
{:value (get context "filter_id")
:onChange #(handle-change % context "filter_id" owner)}
[:option "None Selected"]
(for [[ref option] (:filters meta)]
^{:key ref}
[:option {:value ref} (get option "label")])]] I still get: |
@blissdev I see, I am not sure why it is behaving differently for you. |
As I understand it, Sablono is doing the right thing here, and the warning is warranted. The reason for the warning is that React needs a way to figure out which element is which when the list changes. In the original example in this issue, the (html
[:table
[:tbody
[:tr
(for [k [:last-name :first-name :date]]
[:th (name k)])]]]) Here, the sequence of values is a compile-time constant, (html
[:table
[:tbody
(for [person people]
[:tr
[:td.name (:name person)]])]]) Here, if the first element of I think the best solution is to explicitly opt into splicing for the cases where you're using To those who are comparing Sablono's interpretation with Hiccup's, unless I'm missing something, this isn't a meaningful distinction in Hiccup. It's not a question of just doing what Hiccup does, as Hiccup doesn't compile to |
@Peeja I agree with you. Excellent explanation thanks. |
Having said all that, I do think there's a reasonable case where you don't want the warning: (html
[:div
"submitted on "
(:submission-date data)
(when-let [submitter (:submitter data)]
(list
" by"
(om/build submitter-link submitter)))]) Here we're using a |
The good news is that these cases appear to always use an explicit invocation of (html
[:div
"submitted on "
(:submission-date data)
(when-let [submitter (:submitter data)]
(sablono/splice
" by"
(om/build submitter-link submitter)))]) That could return either a list with metadata or a record which the interpreter could treat differently and splice into the list of children. We could also ask people to supply metadata themselves, but they'd have to use a vector: (html
[:div
"submitted on "
(:submission-date data)
(when-let [submitter (:submitter data)]
^:splice
[" by"
(om/build submitter-link submitter)])]) That can work, since Sablono only considers a vector a React element if the first element in the vector is a keyword, but it still feels icky to use vectors like this. I recommend the first option. |
Actually, that's not quite right. There's one more use case: passing children into another component/element: (defn alert [props & children]
(html
[:.big-alert-box {:class (:type props)}
[:i.alert-icon]
children])) I'm not sure, but I believe React itself solves this by making The upshot is that it would be nice to be able to splice a seq of elements that are already bound to a name. You could actually do that using the proposed (defn alert [params & children]
(html
[:.big-alert-box {:class (:type params)}
[:i.alert-icon]
(apply sablono/splice children)])) I'm not sure how bizarre that would be. |
I'm rendering table headers with om and sablono using a
for
loop:This generates a react warning,
"Each child in an array should have a unique \"key\" prop."
But in this case, I think I just want a TR with some TH children, not a react "array". Is there a way to not generate react components for each element of a for loop, in the case of static data such as above?
Or should I be assigning a unique key to each TH, even in the case of an unchanging sequence?
The text was updated successfully, but these errors were encountered: