1

I don't understand how this can work in javascript

renderMarkButton(type, icon) {

it looks like an arrow function, but without the arrows. Here's the context:

class HoverMenu extends React.Component {

  renderMarkButton(type, icon) {
    const { editor } = this.props
    return (
      <div className="editorButton" 
            onMouseDown={event => this.onClickMark(event, type)}>
        <FontAwesomeIcon color="#666" active={isActive} 
            className="editorButton" icon={icon}  />
        </div>
    )
  }
  render() {
    return (
      <div>
        {this.renderMarkButton('bold', {...faBold})}
      </div>
    )
  }
}

I'm also confused by the

  const { editor } = this.props

which comes from Slate, I believe. I would have expected this.props to be {type,icon} in this case.

Jayavel
  • 3,377
  • 2
  • 21
  • 35
Francis
  • 702
  • 9
  • 21
  • 3
    `renderMarkButton` is a class method, and `const { editor } = this.props` is using 'destructuring' to retrieve `this.props.editor`. – Joe Clay Feb 01 '19 at 10:34

3 Answers3

8

Regarding your questions:

  1. renderMarkButton(type, icon) { is just the es6 class syntax: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
  2. const { editor } = this.props is called "destructuring". You can read about that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

hope that helps :)

Fabian
  • 748
  • 2
  • 11
  • 20
1

Arrow and bound methods are useful for passing them as callbacks to be called later:

<Component onClick={this.clickHandler}/>

This isn't the case for renderMarkButton because it's called in a place where it's used with correct this context:

this.renderMarkButton('bold', {...faBold})

renderMarkButton is class prototype method. It doesn't work like arrow function because it isn't bound to the context. Calling it with wrong context would result in error because there would be no this.props object:

const unboundFunction = this.renderMarkButton;
unboundFunction('bold', {...faBold});
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
0

This is a special syntax according to new class keyword which will let you create classes.

Basically, those are methods of that specific class and you can't define any other method outside of the class using that specific syntax.

For more information, consider MDN as your best partner.

mrReiha
  • 908
  • 6
  • 24