Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

React Create & Use Helper Functions | React Component

How to use Helper functions in a React component?
Create a helper function is a very common practice to achieve modularity. However, you can use it for code reusability. 

You can use Helper Functions just like other frameworks. Also, you can achieve code reusability by using these functions.

Explore React 63 Best FAQs

In the blow example, we have to create a helper class and use to it.
As an Example,

-  Helpers.js
  export function plus(xy) {
    return x + y;
  }

  export function minus(xy) {
    return x - y;
  }

  export function multiply(xy) {
    return x * y;
  }

  export function divide(xy) {
    return x / y;
  }

Import and Use of Helper methods in calculator components,

import React from 'react';
import { plusminus } from './Helpers'

class CalculatorComponents extends React.Component {
    constructor(props){
        super(props);
        this.yourClickFunction=this.yourClickFunction.bind(this);
    }

    yourClickFunction(xy){
      console.log(plus(xy)); //Output will be (10, 20) => 30
      console.log(minus(xy)); //Output will be (10, 20) => -10
    }

    render() {
        return (
            div>
                h4>React Helper functionsh4>
                button onClick={this.yourClickFunction(1020)}>Click..button>
            div>
        );
    }
}
export default CalculatorComponents;


The result of these helper’s methods –



This post first appeared on Programming, please read the originial post: here

Share the post

React Create & Use Helper Functions | React Component

×

Subscribe to Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×