Closed Luiz-Monad closed 5 years ago
Yes, this is a problem we've had several times. You can pass the key in the props, but it's true that most people forget and it'd be nice to have it forced as a parameter. I'd personally avoid many FunctionComponent.Of
variants because it can confuse users. Maybe we could force 'Props
to implement key
through an interface (although that would be a breaking change) or we could add an optional argument for a projection to get the key from the props, like:
static member Of(render: 'Props->ReactElement,
?displayName: string,
?memoizeWith: 'Props -> 'Props -> bool,
?withKey: 'Props -> string) =
...
fun props ->
#if FABLE_COMPILER
let bindKey props =
match withKey with
| Some f -> props?key <- f props; props
| None -> props
#else
let bindKey = id
#endif
ReactElementType.create elemType ( bindKey props ) []
Maybe we could force 'Props to implement key through an interface (although that would be a breaking change)
I don't think a Key
is always needed. It depends on the context of the component.
we could add an optional argument for a projection to get the key from the props
I personally prefer this option :)
Yes, the optional argument would remind you about the key if you needed it.
I was having that pesky problem with React destroying all my DOM instead of patching it until I put key on my function components, including the root one used by Elmish-React.
I solved it this way:
I was thinking if it would be a good thing to expose a key, something like this perhaps?