5

I've just started learning ReactJS and there's this thing that occurred to me.

For example:

The function that I would like to execute against a reactjs element:

function initializeInput(selector, color) {
    // just an example function
    $(selector).css("font-size", "21pt");
}

And a part of my .jsx file:

var myInput = React.createClass({
componentDidMount: function () {
    initializeInput("#" + this.props.inputId);
},
render: function() {
    return (
        <input type="text" value="text goes here" 
               name={this.props.inputName} 
               id={this.props.inputId}/>
    );
}
});

This function is called successfully, however nothing happens and it seems that things don't just work out that way with jQuery and React. Is it even good to mix them up like that? Thanks.

2
  • examine $('selector') it might be empty Commented Jun 25, 2016 at 7:03
  • When I log it in the console it is the appropriate selector... Commented Jun 25, 2016 at 7:06

3 Answers 3

5

Jquery works very well with Reactjs . You can call jquery function after react render ie in componentDidMount and

You can react refs for that. Lets say you want a tooltip

var myInput = React.createClass({
componentDidMount: function () {
    $(this.refs.tooltip).tooltip();
},
render: function() {
    return (
        <input ref="tooltip" type="text" value="text goes here" 
               name={this.props.inputName} 
               id={this.props.inputId}/>
    );
}
});
Sign up to request clarification or add additional context in comments.

5 Comments

Can you demonstrate with some example code, please?
I get an error saying findDOMNode() is not a function, strange :(
just remove react.findDomnode its been deprecated
Thank for noting that out! I will accept your answer since you were the first one to write it. However can you explain why do I have to use refs instead of selecting the items with a regular css selector?
Generally not recommended in react. Using refs ( they are just dom nodes) are the best way to access dom value
2

First you need to render component elements (render function) then call your jquery code in componentDidMount handler

http://jsbin.com/zupagav/1/edit?js,console,output

Comments

2

I believe using this.refs is the way. I don't think you should need React.findDomNode. More about refs from the React docs.

var myInput = React.createClass({
  componentDidMount: function () {
    initializeInput(this.refs.inputId);
  },

  render: function() {
    return (
      <input 
        ref="myInput"
        type="text" 
        value="text goes here" 
        name={this.props.inputName} 
        id={this.props.inputId} />
    );
  }
});

This is useful for use with third party libraries like jQuery, d3, doing canvas stuff, and more.

2 Comments

Thanks for your answer... I gave it a vote up but since #Piyush.kapoor was first I think it is fair that he gets the accepted answer.
I have a .js file in my public folder how do I call the function in that file from a component using jQuery?

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.