From 7269385786819368e878bbe1c11226a4073c6c85 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 4 Sep 2015 12:13:55 +0930 Subject: [PATCH] Make a copy of props passed into a component Prevents some rare errors where the props object is read-only, and is generally safer. --- js/lib/Component.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/js/lib/Component.js b/js/lib/Component.js index 1174599b0..9c622129e 100644 --- a/js/lib/Component.js +++ b/js/lib/Component.js @@ -156,15 +156,17 @@ export default class Component { * @public */ static component(props = {}, children) { - if (children) props.children = children; + const componentProps = Object.assign({}, props); - this.initProps(props); + if (children) componentProps.children = children; + + this.initProps(componentProps); // Set up a function for Mithril to get the component's view. It will accept // the component's controller (which happens to be the component itself, in // our case), update its props with the ones supplied, and then render the view. const view = (component) => { - component.props = props; + component.props = componentProps; return component.render(); }; @@ -177,17 +179,17 @@ export default class Component { // attach a reference to the props that were passed through and the // component's class for reference. const output = { - controller: this.bind(undefined, props), + controller: this.bind(undefined, componentProps), view: view, - props: props, + props: componentProps, component: this }; // If a `key` prop was set, then we'll assume that we want that to actually // show up as an attribute on the component object so that Mithril's key // algorithm can be applied. - if (props.key) { - output.attrs = {key: props.key}; + if (componentProps.key) { + output.attrs = {key: componentProps.key}; } return output;