50

I have got some JSX code in a react app like this:

...
 _renderSignOutLink() {
    if (!this.props.currentUser) {
      return false;
    }

    return (
      <a href="#" onClick={::this._handleSignOutClick}><i className="fa fa-sign-out"/> Sign out</a>
    );
...

What does the double colon, ::, mean before calling the function?

Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
Alexander M.
  • 611
  • 1
  • 7
  • 13

1 Answers1

53

The :: is a proposed binding operator that desugars into a bound function:

::foo.bar
// becomes
foo.bar.bind(foo)

This is useful in React (and any other event handlers) because it means this will have the expected value (instance of the class) when the event handler is later invoked.

ssube
  • 41,733
  • 6
  • 90
  • 131
  • 7
    Just some food for thought. It's not considered good practice to use bind for function calls on components in our render methods, as it can hit at performance. Here are some alternatives https://daveceddia.com/avoid-bind-when-passing-props/ – Justin Jan 26 '17 at 15:55
  • Cory House used to say it [doesn't matter much](https://medium.freecodecamp.org/react-binding-patterns-5-approaches-for-handling-this-92c651b5af56) for most apps, but now he's [changed his mind](https://medium.freecodecamp.org/why-arrow-functions-and-bind-in-reacts-render-are-problematic-f1c08b060e36). – Jeff Lowery May 03 '18 at 22:42