mirror of
https://github.com/flarum/framework.git
synced 2025-05-30 12:05:47 +08:00
Make a copy of props passed into a component
Prevents some rare errors where the props object is read-only, and is generally safer.
This commit is contained in:
@ -156,15 +156,17 @@ export default class Component {
|
|||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
static component(props = {}, children) {
|
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
|
// 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
|
// 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.
|
// our case), update its props with the ones supplied, and then render the view.
|
||||||
const view = (component) => {
|
const view = (component) => {
|
||||||
component.props = props;
|
component.props = componentProps;
|
||||||
return component.render();
|
return component.render();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -177,17 +179,17 @@ export default class Component {
|
|||||||
// attach a reference to the props that were passed through and the
|
// attach a reference to the props that were passed through and the
|
||||||
// component's class for reference.
|
// component's class for reference.
|
||||||
const output = {
|
const output = {
|
||||||
controller: this.bind(undefined, props),
|
controller: this.bind(undefined, componentProps),
|
||||||
view: view,
|
view: view,
|
||||||
props: props,
|
props: componentProps,
|
||||||
component: this
|
component: this
|
||||||
};
|
};
|
||||||
|
|
||||||
// If a `key` prop was set, then we'll assume that we want that to actually
|
// 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
|
// show up as an attribute on the component object so that Mithril's key
|
||||||
// algorithm can be applied.
|
// algorithm can be applied.
|
||||||
if (props.key) {
|
if (componentProps.key) {
|
||||||
output.attrs = {key: props.key};
|
output.attrs = {key: componentProps.key};
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
|
Reference in New Issue
Block a user