Yang03 / blog

0 stars 0 forks source link

decorators in react #20

Open Yang03 opened 6 years ago

Yang03 commented 6 years ago
@setTitle('test')
class Profile extends React.components {

}

function setTilte(title) {
    return (target as WrappedComponent, name, descriptor) => {
        return class extends React.Component {
                   updateTitle = (props) => {
                 // Check if the callback has returned something,
                  // and if so - update the title
                const title = getTitle(props)
                if(title) {
                    document.title = title
                }
            }
            componentDidMount() {
                this.updateTitle(this.props)
            }
            componentWillReceiveProps(props) {
                this.updateTitle(props)
            }
            render() {
                 return <WrappedComponent {...this.props} />
            }
         }
    }
} 
Yang03 commented 6 years ago

js decorators