-
Notifications
You must be signed in to change notification settings - Fork 86
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
Warning: Each child in a list should have a unique "key" prop. #6
Comments
@zanaptak Thanks for filing the issue! @MangelMaxime I don't get it, where is the problem coming from exactly? The only thing I can think of, is that we should not generate a list of BADReact.createElement(div, null, [
React.createElement("h1", null, "Content"),
React.createElement("h2", null, "More content")
]) GOODReact.createElement(div, null,
React.createElement("h1", null, "Content"),
React.createElement("h2", null, "More content")
) But I am not sure how to compile a list into a spread in Fable, is that what that |
Hum, I didn't saw that there is 2 versions of all the HTML element the helpers... And saw only the one re-creating the children. I am not sure about this 2 helpers thing. I suspect you added it here in order to not force the user to create a list if he have only one direct child. But it makes the code less consistant and refactoring harder because the code is not always the same. |
Yes this is indeed what |
@Zaid-Ajaj I can't find a way to use the spread syntax when using the It's really strange because when I dump the instance values between our version and a spread version they both don't have the A possible solution is to use
type ReactChildren =
abstract toArray: ReactElement -> ReactElement seq
abstract toArray: ReactElement seq -> ReactElement seq
type IReactApi =
abstract createElement: comp: obj * props: obj -> ReactElement
abstract createElement: comp: obj * props: obj * [<ParamList>] children: ReactElement seq -> ReactElement
abstract Children : ReactChildren
[<Global("React")>]
let reactApi : IReactApi = jsNative
type prop =
static member children (elems: ReactElement seq) =
Interop.mkAttr "children" (reactApi.Children.toArray elems) Like that React will add the missing let view =
Html.div [
// attr.className "button"
attr.children [
Html.div [
attr.content "1"
attr.key "custom-key"
]
Html.div [
attr.content "2"
]
]
] gives: |
Yes,
|
Thanks for explanation @alfonsogarciacaro, this works nicely when the list is in it's own parameter but not when it is a property of a larger object because it wouldn't make sense like @MangelMaxime said:
I was thinking of extracting the children property from props and then apply to a parameter with @MangelMaxime your solution looks great! I will give it a shot 😄 |
Warnings are gone! thanks a lot @MangelMaxime @zanaptak Can you give it a try in v0.19? |
Yes, you can extract the let [<Emit("$0 in $1")>] isKeyIn (key: string) (o: obj): bool = jsNative
let [<Emit("delete $1[$0]")>] deleteKeyFrom (key: string) (o: obj): unit = jsNative
module Interop =
let createElement name (properties: IReactProperty list) : ReactElement =
let props = keyValueList CaseRules.LowerFirst properties // Inlining only this part can help optimizations from the compiler
if isKeyIn "children" props then
let children = props?children
deleteKeyFrom "children" props
ReactBindings.React.createElement(name, props, children)
else
ReactBindings.React.createElement(name, props, []) BTW, the |
@alfonsogarciacaro The solution I proposed don't ignore the It asks to React to provide the key it's using internally if not is set. That's why you see:
I didn't test if there is any performance penalty by this solution but I don't think there is. If there is we can indeed, try your proposed solution. :) |
Yeah we can try the solution Alfonso proposed, for now this is the simplest one without helper code 😄 |
@Zaid-Ajaj Yep fixed, thanks. 👍 |
It seems that each level of nesting is always producing a
Warning: Each child in a list should have a unique "key" prop.
in the browser console.In old DSL I believe this only happened with
ofList
helper.Workaround is to put
prop.key
on every element.Repro:
Produces:
The text was updated successfully, but these errors were encountered: