Last active
March 16, 2016 04:43
-
-
Save roman01la/ca29d6e2ad80a6a92864 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Nav extends React.Component { | |
constructor() { | |
super(); | |
this.onNav = this.onNav.bind(this); | |
} | |
onNav(idx) { | |
// do something with `this` and `idx` | |
} | |
render() { | |
return ( | |
<ul> | |
{btns.map((btn, i) => <Button key={btn} onClick={this.onNav} idx={i}>{btn}</Button>)} | |
</ul> | |
); | |
} | |
} | |
class Button extends React.Component { | |
shouldComponentUpdate(nextProps) { | |
return !shallowEqual(nextProps, this.props); | |
} | |
render() { | |
const { onClick, idx, children } = this.props; | |
return <button onClick={() => onClick(idx)}>{children}</button>; | |
} | |
} |
Nope, that won't work because `onNav()`` is returning a new function on every call anyway.
@hccampos is this what the author means by don't partially apply when passing them to components?
@roman01la eslint with enabled jsx-no-bind rule does not allow syntax at line 24 as well:
return <button onClick={() => onClick(idx)}>{children}</button>;
Is it ok in such cases?
@adaniliuk I believe so in this case. You aren't creating a new function.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you do something like:
And that way you don't need to pass
idx
to the<Button>
component?