JavaScript React where in the component to add a simple math function react component where to fetch data,react component,react component

I have a simple react component to which I want to add a simple math function named doMath that uses property values.

class MyClass extends Component {
    constructor( props ) {
        super( ...arguments );
    }

    render() {
        const { attributes } = this.props; /* attributes passed from parent component */
        const { width, height } = attributes;

        return (
           /* render something using the width and height values */
        )
    }
}

I can add the doMath function in the following ways...

Inside the class:

class MyClass extends Component {
    constructor( props ) {
        super( ...arguments );
    }

    doMath() {
       const { width, height } = this.props.attributes;
       const result = 100 / (width / height);
       return result;
    }

    render() {
        const { attributes } = this.props; /* attributes passed from parent component */
        const { width, height } = attributes;

        return (
           doMath();
        )
    }
}

Outside the class as a const:

const doMath = (width, height) {
  const result = 100 / (width / height);
  return result;
}

class MyClass extends Component {
    constructor( props ) {
        super( ...arguments );
    }

    render() {
        const { attributes } = this.props; /* attributes passed from parent component */
        const { width, height } = attributes;

        return (
           doMath(width, height);
        )
    }
}

Inside render as a const:

class MyClass extends Component {
    constructor( props ) {
        super( ...arguments );
    }

    render() {
        const { attributes } = this.props; /* attributes passed from parent component */
        const { width, height } = attributes;

        const doMath = (width, height) {
           const result = 100 / (width / height);
           return result;
        }

        return (
           doMath(width, height);
        )
    }
}

Seems like I can also add it to componentDidMount or componentDidUpdate

From what I understood it is bad practice to add it in render() but it seems to work everywhere. What is the best practice in this situation?

Answer:1

The best way of these 3 is inside the class, since then other components can reuse that component with the math function. If you do that outside of the component this function is no longer tied to the component. However, in the future it is very likely that Class components will be obsolete and will no longer receive updates since Hooks are the new thing that make all the components have state. If function was stateless before it will be stateful in the hooks version and you should read about it it is a great thing!

Answer:2

I'm creating a 12 days of Christmas javascript program and when I print out the statement it keeps repeating the statement. Can you give me any suggestions on how to fix this and get the program to ...

I'm creating a 12 days of Christmas javascript program and when I print out the statement it keeps repeating the statement. Can you give me any suggestions on how to fix this and get the program to ...

I would expect the following code to create an element which contains a div with the text "Hi" in it. The element appears in the inspector, but no text is visible on the screen. When I change the ...

I would expect the following code to create an element which contains a div with the text "Hi" in it. The element appears in the inspector, but no text is visible on the screen. When I change the ...

I have HTML like below <ul id="tabStripSecondaryElements" class="k-tabstrip-items k-reset" data-role="tabstrip" style="margin-left: 37px; margin-right: 40px;" role="tablist" aria-activedescendant="...

I have HTML like below <ul id="tabStripSecondaryElements" class="k-tabstrip-items k-reset" data-role="tabstrip" style="margin-left: 37px; margin-right: 40px;" role="tablist" aria-activedescendant="...

  1. find inner element jquery
  2. find inner element javascript
  3. find inner element by id jquery
  4. find element by inner text
  5. find element by inner text selenium
  6. find element by innerhtml
  7. find element by innertext javascript
  8. find element by inner text jquery
  9. selenium find element by innerhtml
  10. selenium find inner element
  11. jquery find inner element by class
  12. js find inner element
  13. css find inner element
  14. jest find inner element
  15. jquery find element by innerhtml
  16. js find element by inner text
  17. xpath find element by inner text
  18. how to find inner element

I have the following array: const x = [ {event: "oninput", action: ""}, {event: "onfocus", action: ""} ] Following is the desired output: // event: action { oninput: "", onfocus: "" ...

I have the following array: const x = [ {event: "oninput", action: ""}, {event: "onfocus", action: ""} ] Following is the desired output: // event: action { oninput: "", onfocus: "" ...

  1. convert array object to string
  2. convert array object to array javascript
  3. convert array object to string powershell
  4. convert array object to string javascript
  5. convert array object to list java
  6. convert array object to json
  7. convert array object to array php
  8. convert array object to list
  9. convert array object to list c#
  10. convert array object to string python
  11. convert array object to string c#
  12. convert array object to string php
  13. convert array object to object
  14. convert array object to list python
  15. convert array object to json php
  16. convert array object to object typescript
  17. convert array object to string jquery
  18. convert array object to json jquery
  19. convert array object to array python
  20. convert array object to json in javascript