0

I am trying to call a javascript function from react in the process of adapting a "React" based "Menu" on some old function previously written with jquery

below is my code for the menu

 class Menus extends React.Component{
      constructor(props) {
      super(props);
      this.state = {
      visible: false,
      data: []
    };
  }

  componentDidMount(){
    var _this = this;
    this.setState({data: this.props.data});
  }

  handleClick () {
    var active = !this.state.visible;
    this.setState({ visible: active });
    console.log(this.props.data.MenuText);
  }

  menuTrigger(params){ 
    var func="testalert";//this.props.data.onclick;
    var fnparams = JSON.parse(this.props.data.Parameter);
    // find object
    var fn = eval(func);

    // is object a function?
    console.log('typeof fn === "function":',typeof fn === "function" , typeof fn,fnparams);

            fn.apply(null,fnparams); 
    console.log('Executing:',this.props.data.onclick);
  }
  render(){
    var childNodes;
    var classObj;
    var url;
    childNodes = null;

    if (this.props.data.Children.length) {
      childNodes = this.props.data.Children.map(function(node, index) {
        return  <Menus data={node} />
      });
      classObj = {
        togglable: true,
        "togglable-down": this.state.visible,
        "togglable-up": !this.state.visible
      };
    }

    var style;
    if (!this.state.visible) {
      style = {display: "none"};
    }
    if (childNodes != null){
      return (
        <li>
          <a href="#" onClick={this.handleClick.bind(this)} className="menu {classNames(classObj)}">
            <i className="fa fa-road"></i>
            {this.props.data.MenuText}
          </a>
          <ul style={style} className="treeview-menu">{childNodes}</ul>
        </li>
      );
    }else{
        return (
          <li>
            <a href="#" className="menu" onClick={() => this.menuTrigger(this.props.data.Parameter)}>
                <i className="fa fa-files-o"></i>
               {this.props.data.MenuText}
           </a>
          </li>
        );
    }
  }
}

the javascript function name is stored in a string format in a json object

var example={ 
"MenuText": "example", 
"Parameter": "{'name':'joe'}",
"onclick": "sayhello"
};

the previous code is implement as such

function sayhello(parameter){
   alert('hi '+parameter.name);
}

i have been trying to execute this but getting an undefined on "parameter".

I am in doubt of few matters here.

  1. why is params empty ? the string is accepting it as "function"

    fn.apply(null,fnparams);

  2. is this a rightful way to use react ?

2
  • 1
    At start you should try to debug (or to learn how to debug) Commented Jan 3, 2017 at 12:06
  • add into your constructor this: this.menuTrigger = this.menuTrigger.bind(this); Commented Jan 3, 2017 at 12:42

1 Answer 1

1

Why not use:

   var func="testalert";//this.props.data.onclick;
    var fnparams = JSON.parse(this.props.data.Parameter);
    // find object
    eval(func+'('+fnparams+')');
Sign up to request clarification or add additional context in comments.

4 Comments

Its not really the right way to use it in react. Im not really sure why you are passing the function as string. If you can pass it as itself (function object) then you can simply call it with parameters (no need for apply or anything)
thanks. the function to be executed is stored in a database, I dont get it what you mean by function object. I am learning and keen on doing it properly, mind showing me an example ?
If the name of the function is stored in a DB then i guess that's the only way to do it. But if its not, by function object i mean the actual reference to the function. Lets say you have somewhere in the code - var x = function () {...}, then you can call it using the variable x, like so: x(fnparams)
Thanks @Kinnza . I will use the "eval(func(param))" method for now. Until i learn more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.