1

I'm new to reactjs, I have stuck in between please help me out here :

I have a class component in react js. And I have <ul></li></li></ul>, here I'm adding <li> from jquery after some DB query. Now how can I add onClick event to li ? bcz when I'm adding it from jquery i.e it's giving 'undefined function this.getValue' in react js.

Here is below example :

class getData extends React.Component {

    constructor(props) {
        super(props);
    }

    getValue(p1,p2,p3) {
        .... 
    }

    render() {

        var dropdown = <ul id="dropD"></ul>

        return (
            {dropdown}
        )
    }
} 

Now from Jquery, I'm get li with some DB query and appending it to ul as below

<li onClick=this.getValue('x','xx','xxx')>value 1</li>
<li onClick=this.getValue('x','xx','xxx')>value 2</li>
<li onClick=this.getValue('x','xx','xxx')> ... </li>

Here I'm getting all the dropdown properly, but when I click on dropdown (li) tag, I'm getting as 'undefined this.getValue function' because I'm adding it dynamically. If I hard code the li under ul, it is able to recognize the function and is working properly, only issue when I add li dynamically.

Please let me know if I add li tag dynamically then how can I call onClick function inside class component

4
  • instead of adding onClick this that, add this: onClick={this.getValue.bind(this,'x','xx','xxx')}, it should work. Commented Apr 24, 2017 at 11:39
  • You're trying to refer to the class instance from outside of the class. Instead add those <li>s declaratively inside your React component class. Commented Apr 24, 2017 at 11:49
  • Hi Mayank, I'm adding onClick event from jquery itself. when I added your code I'm getting error as "Cannot read property 'bind' of undefined" Commented Apr 24, 2017 at 11:56
  • @AsafDavid, yes your right, but the class component has multiple loop functions. I cannot call ajax DB query each and every loop that's the reason I took out of react js and wanted to append from jquery with only 1 time ajax call. Commented Apr 24, 2017 at 11:59

1 Answer 1

1

Using jQuery to modify your react DOM tree is considered an anti-pattern and you should not do it. This is only going to cause you a world of pain when React attempts to compare against the virtual DOM. In fact, I'm surprised React is not warning you of this already.

What is the reason you do not want to add the new li children from within React itself?

Also, another thing is jumping out at me too, your component in this example is called getData. What is this component designed to do? It seems like a strange name to use.

If you are trying to populate a drop down I suggest you use componentDidMount to do your database call and update your view data on success.

example

class MySelectBox extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            items: ['Orange', 'Banana', 'Apple']
        };
    }

    componentDidMount() {
        window.$.ajax({
            url: "/api/getMyItem"
        }).done(function(result) {
            this.setState((state) => {
                state.items.push(result);
                return state;
            });
        });
    }

    handleClickEvent(e, value) {
        .... 
    }

    render() {
        return (
            <ul>
                {this.state.items.map((label) => {
                    return (
                        <li onClick={this.handleClickEvent.bind(this, label)}>
                            {label}
                        </li>
                    );
                })}
            </ul>
        );
    }
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Jeenas, thanks for your code. You have cleared my way where I was stuck from long time. But can you please explain me about ajaxMyItems.then((results) ? Instead of that can I add my normal ajax call i.e $.ajax({...}) and update the state ? . I tried by adding ajax call but didn't got the output so thought to bother you.
Hey @B77 I updated my answer to include a jQuery ajax example. I used window.$ as I assume you are importing a jQuery script in the HTML. If you are using webpack or browserify you should remove the window. bit and add import $ from 'jquery' at the top of your file. Also i know your starting out, but AJAX stuff should be moved into "actions" in conjunction with Flux or Redux.. but do that when you're more comfortable with this.

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.