0

I am getting function name in the form of a string from the backend i need to add it to onClick event of a button in react but getting error ReactJS: Expected onClick listener to be a function, instead got type string how can i add the string to onClick function

response object from backend :

[
{option : {onClickFunc : "getReleaseNotes"}}
],

React on front end

<div onClick={option.onClickFunc}>
</div>

error on click :

ReactJS: Expected onClick listener to be a function, instead got type string

1 Answers1

0

You can use eval

eval(option.onClickFunc)();

or add getReleaseNotes to an object and then get it with accessor

myFunctions[option.onClickFunc]();

Look at this post How to execute a JavaScript function when I have its name as a string for more options.

Leopold Stoch
  • 51
  • 1
  • 3