简体   繁体   中英

Object assign on immediately created object when spread

I am a bit tired of passing react props this way

<Comp one={one} two={two} three={three} />

So I started doing

<Comp {...{one, two, three}} />

However I realized that this computes to Object assig

react.createElement(Comp , Object.assign({
    one: one,
    two: two
  }, {
    __source: {
      fileName: _jsxFileName,
      lineNumber: 95
    },

Since I'm spreading an object that I'm just creating I expected the compiler/transpiler to be smart enough to realize about that and do something like this instead

react.createElement(Comp ,{
    one: one,
    two: two,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 95
    },

Is there any Babel plugin to accomplish this, or any way of compiling this way ?

Regards

The Babel version and settings you use are pretty important.

Using the latest, I had this code:

return (<Child {...{ name: this.name }} />);

Which turned into this:

return Object(m.a)(t, e), Object(c.a)(t, [{
    key: "render",
    value: function() {
        return o.a.createElement(k, {
            name: this.name
        })
    }
}])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM