From 2447474438829217d1048111d422c0d776758e9d Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 25 Oct 2022 11:42:08 +0200 Subject: [PATCH] Fix addKeyProp to not mutate props object --- src/React.bs.js | 4 ++-- src/React.res | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/React.bs.js b/src/React.bs.js index 5eb6a50..5531373 100644 --- a/src/React.bs.js +++ b/src/React.bs.js @@ -9,7 +9,7 @@ function component(prim) { } function createElementWithKey(key, component, props) { - return React.createElement(component, key !== undefined ? Object.assign(props, { + return React.createElement(component, key !== undefined ? Object.assign({}, props, { key: key }) : props); } @@ -17,7 +17,7 @@ function createElementWithKey(key, component, props) { function createElementVariadicWithKey(key, component, props, elements) { return Caml_splice_call.spliceApply(React.createElement, [ component, - key !== undefined ? Object.assign(props, { + key !== undefined ? Object.assign({}, props, { key: key }) : props, elements diff --git a/src/React.res b/src/React.res index 69d967d..40167dd 100644 --- a/src/React.res +++ b/src/React.res @@ -15,10 +15,13 @@ type component<'props> = Jsx.component<'props> let component = Jsx.component %%private( + @val + external propsWithKey: (@as(json`{}`) _, 'props, {"key": string}) => 'props = "Object.assign" + @inline let addKeyProp = (~key: option=?, p: 'props): 'props => switch key { - | Some(key) => Obj.magic(Js.Obj.assign(Obj.magic(p), {"key": key})) + | Some(key) => propsWithKey(p, {"key": key}) | None => p } )