React component with dynamically created function

kliron

I have a React component that includes an input element. When the value of the input changes the handler function is supposed to validate the input and act accordingly. The actual validator is dynamically created (via new Function(code)) with the actual code passed as a string prop to the element. The validation code never changes for a given component.

Right now I am doing the function construction in the actual onChange handler which seems unnecessary. It does not belong in state either. I would like to create the function once, store it somewhere and use it on demand. My question is, where should it be stored? Should I make it an attribute of the actual component object? The statics object seems also reasonable, but can one pass properties dynamically (like the code string above) and if yes, how?

hugomg

Recomputing the validator inside the onchange is not that bad from a reproducibility point of view. It only affects performance and if that is an issue one possibility would be to use the caching mechanism of your choice:

handleOnChange: function(){
    if(this.cachedValidatorString !== this.props.validatorString){
        this.cachedValidatorString = this.props.validatorString;
        this.cachedValidator = new Function(...);
    }

    // ...
}

Another, perhaps cleaner, approach would be to update the validatorFunction field inside the render method. That is the earliest you can update it and it guarantees that it will always correspond to your current props.

render: function(){
   this.validatorFunction = new Function(this.props.validatorString);

   return <input onChange={...} />;
}

As for the other possibilities you mentioned:

I agree that this.state is not the best place to put the validator function because in general you want to avoid putting things that can be computed from the props in the state. See props in getInitialState as an anti pattern

Finally, I don't think statics would make much sense here. Static properties are shared by all instances of your class but the validator functions need to be different for each instance of the component.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React component with dynamically created function

From Dev

Reference dynamically created component in Aurelia

From Java

Dynamically Rendering a React component

From Dev

Create react component dynamically

From Dev

passing, dynamically created objects, to a function

From Dev

jquery function for images created dynamically

From Dev

Bind function to dynamically created element

From Dev

on click function to dynamically created button

From Dev

Dynamically ended function for dynamically created video tag

From Dev

Attach component to dynamically created elements with Twitter Flight

From Dev

How to add styles to dynamically created component

From Dev

Dynamically add another component in react

From Dev

Dynamically render a component inside a another component in React

From Dev

Dynamically Rendering a React Component in React 0.12

From Dev

Dynamically insert react components into another react component

From Dev

Call jQuery function on object dynamically created by Angular

From Dev

Control Dynamically created listbox property in other function

From Dev

How to pass an object in a dynamically created function call

From Dev

Unable to bind function on dynamically created element

From Dev

call a javascript function within dynamically created html

From Dev

Call controller function from HTML dynamically created

From Dev

Jquery function on Dynamically created List not working

From Dev

Which dynamically created object called the function?

From Dev

Click function not working with dynamically created selector on Safari

From Dev

Inherit from class dynamically created in a function

From Dev

Jquery function on Dynamically created List not working

From Dev

Call custom function on dynamically created elements with jQuery

From Dev

intellisense.annotate a function with a dynamically created annotation

From Dev

Which dynamically created object called the function?

Related Related

  1. 1

    React component with dynamically created function

  2. 2

    Reference dynamically created component in Aurelia

  3. 3

    Dynamically Rendering a React component

  4. 4

    Create react component dynamically

  5. 5

    passing, dynamically created objects, to a function

  6. 6

    jquery function for images created dynamically

  7. 7

    Bind function to dynamically created element

  8. 8

    on click function to dynamically created button

  9. 9

    Dynamically ended function for dynamically created video tag

  10. 10

    Attach component to dynamically created elements with Twitter Flight

  11. 11

    How to add styles to dynamically created component

  12. 12

    Dynamically add another component in react

  13. 13

    Dynamically render a component inside a another component in React

  14. 14

    Dynamically Rendering a React Component in React 0.12

  15. 15

    Dynamically insert react components into another react component

  16. 16

    Call jQuery function on object dynamically created by Angular

  17. 17

    Control Dynamically created listbox property in other function

  18. 18

    How to pass an object in a dynamically created function call

  19. 19

    Unable to bind function on dynamically created element

  20. 20

    call a javascript function within dynamically created html

  21. 21

    Call controller function from HTML dynamically created

  22. 22

    Jquery function on Dynamically created List not working

  23. 23

    Which dynamically created object called the function?

  24. 24

    Click function not working with dynamically created selector on Safari

  25. 25

    Inherit from class dynamically created in a function

  26. 26

    Jquery function on Dynamically created List not working

  27. 27

    Call custom function on dynamically created elements with jQuery

  28. 28

    intellisense.annotate a function with a dynamically created annotation

  29. 29

    Which dynamically created object called the function?

HotTag

Archive