Creating custom function in React component

前端 未结 4 674
南旧
南旧 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 <button onClick={this.handleClick.bind(this)} value="Click me" />;
        }
    }
    
    0 讨论(0)
  • 2021-01-30 20:33

    Another way:

    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 <button onClick={this.handleClick} value="Click me" />;
      }
    }
    

    In this format you don't need to use bind

    0 讨论(0)
  • 2021-01-30 20:36

    With React Functional way

    import React, { useEffect } from "react";
    import ReactDOM from "react-dom";
    import Button from "@material-ui/core/Button";
    
    const App = () => {
      const saySomething = (something) => {
        console.log(something);
      };
      useEffect(() => {
        saySomething("from useEffect");
      });
      const handleClick = (e) => {
        saySomething("element clicked");
      };
      return (
        <Button variant="contained" color="primary" onClick={handleClick}>
          Hello World
        </Button>
      );
    };
    
    ReactDOM.render(<App />, document.querySelector("#app"));
    

    https://codesandbox.io/s/currying-meadow-gm9g0

    0 讨论(0)
  • 2021-01-30 20:54

    You can try this.

    // Author: Hannad Rehman Sat Jun 03 2017 12:59:09 GMT+0530 (India Standard Time)
    
    import React from 'react';
    import RippleButton from '../../Components/RippleButton/rippleButton.jsx';
    
    class HtmlComponents extends React.Component {
    
        constructor(props){
            super(props);
            this.rippleClickFunction=this.rippleClickFunction.bind(this);
        }
    
        rippleClickFunction(){
            //do stuff. 
            // foo==bar
        }
    
        render() {
          return (
             <article>
                 <h1>React Components</h1>
                 <RippleButton onClick={this.rippleClickFunction}/>
             </article>
          );
       }
    }
    
    export default HtmlComponents;
    

    Yhe only concern is you have to bind the context to the function

    0 讨论(0)
提交回复
热议问题