-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JSX] Attribute shorthand as es6 Objects #2777
Comments
I'd argue against this, as one would have to consider meaningless statements such as Also, jstransform should already allow for the object initializer shorthand outside of JSX attributes. |
@ToucheSir mistakes like that you described, could be done with spread objects, but it was included in core.. |
How so? Attempting to use |
@ToucheSir like this -> |
@vanesyan it's possible with the ES6 option : <MyComponent {...{item, key}} /> compiles to React.createElement(MyComponent, React.__spread({}, {item:item, key:key})) I don't really get why the spread works on props "top-level" while this doesn't, but using an plain object does the job. ref facebook/jsx#23 |
@bloodyowl Good example. Would agree with @RReverser 's reasoning in the linked issue (explicit destructuring is less ambiguous and captures the process more effectively), but I suppose it's a rather subjective matter. |
I feel like we've said no to this with good reason before but I can't find the link. Regardless, the discussion for extending semantics of JSX belongs in the issue @bloodyowl linked to. @sebmarkbage, perhaps you remember where we've talked about this before / can weigh in. |
This is something that would be really handy in my opinion. render() {
const { foo, bar, spam, eggs } = this.props;
return (
<div>
<Component1 foo={foo} bar={bar} />
<Component2 spam={spam} eggs={eggs} />
</div>
);
} I don't really like using Too bad that Maybe this alternative is good enough, though: render() {
const { foo, bar, spam, eggs } = this.props;
return (
<div>
<Component1 {...{foo, bar}} />
<Component2 {...{spam, eggs}} />
</div>
);
} |
With https://github.com/RReverser/es-borrowed-props proposal, you will be able to: render() {
return (
<div>
<Component1 {...{ this.props.foo, this.props.bar }} />
<Component2 {...{ this.props.spam, this.props.eggs }} />
</div>
);
} Until that, I'm afraid, destructuring is your best solution. |
It's common to use blacklist for passing props in wrapper components, so you could use the inverse: whitelist. render() {
const { foo, bar, spam, eggs } = this.props;
return (
<div>
<Component1 {...whitelist(this.props, 'foo', 'spam')} />
<Component2 {...whitelist(this.props, {spam: true, eggs: true})} />
</div>
);
} // unreadable example implementation
const whitelist = (obj, ...args) => _whitelist(obj, _flatten(args.map(_whitelistKeys)));
const _whitelistKeys = (x) => typeof x === 'string' ? [x] : Object.keys(x).filter((key) => x[key])
const _whitelist = (obj, keys) => keys.reduce((acc, key) => { acc[key] = obj[key]; return acc }, {}) ;
const _flatten = (xss) => Array.prototype.concat.apply([], xss); It's probably best to solve this outside react, like blacklist, classnames, and other similar useful modules. |
We're deprecating our own JSX transform so going to close this out. |
Ecvevalent to
It's es6 feature http://wiki.ecmascript.org/doku.php?id=harmony:object_initialiser_shorthand
Sorry for my bad English :)
The text was updated successfully, but these errors were encountered: