Allow arrow functions in JSX props#782
Conversation
|
You should reference the eslint rule (which in this case is also |
|
👎 |
|
@tleunen what about at the bottom level, where you directly create a native DOM element? |
|
What do you mean by that? To me, it's way better to have function ItemList(props) {
return (
<ul>
{props.items.map((item, index) => (
<Item
key={item.key}
item={item}
listIndex={index}
onItemClick={doSomethingWith}
/>
))}
</ul>
);
}And then Item can attach the The downside is that it has to know the list index, but well.. I think it's better than creating a new bound function every time. |
|
@tleunen ok so what does the implementation of |
|
Sure, but you only create it once class Item extends React.Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(e) {
this.props.doSomethingWith(props.item.name, props.listIndex);
}
render() {
return <button onClick={this.handleClick}>Click me</button>
}
} |
|
Right, so the question is really whether |
|
To me, the arrow function seems a lot cleaner: function Item({ doSomethingWith, item, listIndex }) {
return <button onClick={() => doSomethingWith(item.name, listIndex)}>Click me</button>;
} |
|
I know React can apply some performance optimizations on the SFC, but would that be better than having the class without recreating the function at every render? |
|
First, as I have recently learned, the only advantage with the constructor-binding is that Thus this is mostly a question of subjective readability, I think. |
|
Make sense. So creating non-bound functions is fine, but not otherwise. |
|
I am in favor of allowing arrow functions in render methods. We should continue disallowing |
| @@ -42,7 +42,7 @@ module.exports = { | |||
| // https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md | |||
| 'react/jsx-no-bind': [2, { | |||
| 'ignoreRefs': false, | |||
There was a problem hiding this comment.
will we be able to do this, then: ref={foo => this.foo = foo}?
There was a problem hiding this comment.
only if this was set to true, as i understand it
There was a problem hiding this comment.
if arrow fns were allows, seems like we should change that as well?
There was a problem hiding this comment.
I agree, arrows for refs are useful when you want to use inputs in a SFC.
|
@lelandrichardson Would |
|
@tleunen the same as one without. it basically does a |
|
Which is more readable? (A) (B) Use a closure The answer is (B) because you don't need to look at the code of |
|
I agree that B is more readable - however with the rules as-is on master, you'd model that as a stateful component. |
93ccdf7 to
ce7cf9c
Compare
ce7cf9c to
94ace27
Compare
Rationale: