Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with basic React Example: Uncaught TypeError: undefined is not a function

I'm trying to get react connected into my app. Its a rails app using rails-react (though I don't think that's part of the problem). I'm currently using a very simple 1 component setup:

// react_admin.js.jsx  /** @jsx React.DOM */ var CommentBox = React.createClass({   render: function() {     return (      <div className="commentBox">        Hello, world! I am a CommentBox.       </div>    );   } });  React.render(   <CommentBox />,   document.getElementById('content') ); 

My html file contains:

<body>   <div id="content"></div>   <script src="/assets/react.js?body=1"></script>   <script src="/assets/react_admin.js?body=1"></script> </body> 

I can see that rails-react is converting my react_admin.js.jsx into react_admin.js as follows:

/** @jsx React.DOM */  var CommentBox = React.createClass({displayName: 'CommentBox',   render: function() {     return (       React.DOM.div({className: "commentBox"},          "Hello, world! I am a CommentBox."       )     );   } });  React.render(   CommentBox(null),   document.getElementById('content') ); 

However chrome is raising an ''Uncaught TypeError: undefined is not a function'' in the Render.react() call, which it shows between "(" and "CommentBox(null)"

Can someone tell me what I'm doing wrong?

like image 725
Laurie Young Avatar asked Oct 29 '14 10:10

Laurie Young


People also ask

How do I fix uncaught TypeError is not a function?

Another possible solution would be to set the variable $ to noConflict, overriding WordPress' initial scripts like in the example below. var $ = jQuery. noConflict(); $(document). ready(function(){ // jQuery code });

Is not a function error react?

The React. js "Uncaught TypeError: X is not a function" occurs when we try to call a value that is not a function as a function, e.g. calling the props object instead of a function. To solve the error, console. log the value you are calling and make sure it is a function.

Is not a function is undefined?

This is a common JavaScript error that happens when you try to call a function before it is defined. You get this error when you try to execute a function that is uninitialized or improperly initialized . It means that the expression did not return a function object.

Why is my JavaScript function undefined?

A variable that has not been assigned a value is of type undefined . A method or statement also returns undefined if the variable that is being evaluated does not have an assigned value. A function returns undefined if a value was not returned .


2 Answers

After 0.14 React moved to ReactDOM.render(), so if you update React, your code should be:

ReactDOM.render(   <CommentBox />, document.getElementById('content')); 

But make sure to include both react.js and react-dom.js in your project since those are now separated.

like image 78
IVN Avatar answered Sep 23 '22 20:09

IVN


You should update to the latest React library.

Some tutorials were updated to use React.render() instead of the deprecated React.renderComponent(), but the authors are still providing links to older versions or React, which do not have the newest render() method.

like image 43
Sebastián Grignoli Avatar answered Sep 22 '22 20:09

Sebastián Grignoli