Creating custom function in React component

前端 未结 4 675
南旧
南旧 2021-01-30 19:58

I have a React component

export default class Archive extends React.Component { 
   ...
}

componentDidMount and onClick

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 20:33

    You can create functions in react components. It is actually regular ES6 class which inherits from React.Component. Just be careful and bind it to the correct context in onClick event:

    export default class Archive extends React.Component { 
    
        saySomething(something) {
            console.log(something);
        }
    
        handleClick(e) {
            this.saySomething("element clicked");
        }
    
        componentDidMount() {
            this.saySomething("component did mount");
        }
    
        render() {
            return 

提交回复
热议问题