You get a bonus - 1 coin for daily activity. Now you have 1 coin

How do you use decorators in React? in REACT

Practice



You can decorate  your class components , which is equivalent to passing the component into a function. Decorators are a flexible and readable way of changing component functionality.

@setTitle('Profile')
class Profile extends React.Component {
    //....
}

/*
 title is the string that will be set as the document title. 
  WrappedComponent is what our decorator will receive if 
  it is placed directly above the component class, as in the example above 
*/
const setTitle = (title) => (WrappedComponent) => {
  return class extends React.Component {
    componentDidMount() {
      document.title = title
    }

    render() {
      return <WrappedComponent {...this.props} />
    }
  }
}

Note. Decorators are a feature that did not make it into ES7, but is currently a stage 2 proposal .

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Scripting client side JavaScript, jqvery, BackBone"

Terms: Scripting client side JavaScript, jqvery, BackBone