9

From here :

"The only way to get a handle to a React Component instance outside of React is by storing the return value of React.render."

I need to render a React component outside React and the reason for it I'm going to mention below.

In my node.js, expressJS app, I am using 'react-router-component' and 'react-async'.

In app.js -the file which is supposed to be run ,

    var url=require('url');   
    var App=require('./react/App.jsx');
    var app = express();
    app.get('*',function(req,res){


    //}); SEE EDIT 1 BELOW

    var path = url.parse(req.url).pathname;
                ReactAsync.renderComponentToStringWithAsyncState(App({path:path}),function(err, markup) {
    res.send('<!DOCTYPE html>'+markup);

          });
   });

In App.jsx,

   PostList = require('./components/PostList.jsx');
   var App = React.createClass({
   render: function() {
        return (

           <html>
           <head lang="en">
           </head>
        <body>
        <div id="main">

        <Locations path={this.props.path}>

          <Location path="/" handler={PostList} />
          <Location path="/admin" handler={Admin} />
        </Locations>


       <script type="text/javascript" src="/scripts/react/bundle.js"></script>
       <script type="text/javascript" src="/scripts/custom.js"></script>                 

        </body>
        </html>
        }); 

bundle.js is the browserified file from all the .jsx files.

In PostList.jsx,

    var PostList = React.createClass({

        mixins: [ReactAsync.Mixin],

        getInitialStateAsync: function(cb) {

           if(typeof this.props.prods==='undefined'){

            request.get('http://localhost:8000/api/cats_default', function(response) {

                    cb(null, {items_show:response.body});

                       });
                                                    }
        },

        setTopmostParentState: function(items_show){

          this.setState({
             items_show:items_show
                       });

         },

        render: function() {

        return (

              <div className="postList" id="postList">
              **// Things go here** 
              <div className="click_me" >Click me</div>
              </div>    
            }

    });


    PostListRender=function(cart_prods){

        var renderNow=function(){

            //return <PostList  cart_prods={cart_prods}></PostList>

             React.renderComponent(<PostList  cart_prods={cart_prods}></PostList>,document.getElementById('postList') );  
                                 };

         return {
           renderNow:renderNow
                }   
        };
    module.exports=PostList;

In custom.js:

$('.click_me').click(function(){

PostListRenderObj=PostListRender(products_cart_found);
PostListRenderObj.renderNow();

$('odometer').html('price');// price variable is calculated anyhow

});

The page shows well.

EDIT 3 Starts

Now I want to render the PostList component on clicking the click_me div .

EDIT 3 Ends

But when I click on the click_me element, the browser shows script busy, console shows

ReactJS - ReactMount: Root element has been removed from its original container. New container

And the Firebug log limit exceeds.

So why I want to render on click from outside react.js: I have to run the jQuery Odomoeter plugin on clicking the click_me div. The plugin was not developed as a node middleware although it can be installed the way a middleware is installed and the plugin codebase is saved inside node_modules folder.

Edit2 Starts:

As the plugin is not a node middleware, I cannot require it from inside node. However I can perform the click event (code not shown ) from inside node and run the following code there as well :

$('odometer').html('price');// price variable is calculated anyhow

In this case I include the plugin in the browser with <script /> tag and the browserified bundle.js comes after the plugin script . But the animation is not properly shown. So I take to the client side click event in the custom.js.

If I do not require the plugin to be a middleware from inside node and just include it in the page before the browserified JS file and perform the click event inside React, then the odometer animation is not properly shown.

Edit2 Ends:

So what is the way to render the PostList React component outside React ?

EDIT 1 The }); was quite mistakenly placed there

SaidbakR
  • 11,955
  • 16
  • 89
  • 173
Istiaque Ahmed
  • 4,977
  • 17
  • 59
  • 117
  • 6
    It's quite difficult to tell what you're asking/trying to do here, and I think there might have been some copy/paste issues as the code isn't valid as written. Do you have a JSFiddle, GitHub project, or some other actually working (or not-quite-working) example? – Michelle Tilley Jan 13 '15 at 16:51
  • @BrandonTilley, I pasted only the relevant part here. I need only the logic however. What problems do you find ? – Istiaque Ahmed Jan 13 '15 at 16:54
  • @BrandonTilley, The page shows up in browser as expected. Now on clicking a `div` in the page from client side React, I want to re-render the page. This is all I want to do. The `click` event will take place from client side. The reason as to why I do not trigger the `click` from within React is that I have to run a jQuery plugin namely odometer on each click and if I trigger it from within React in server side, I can not run the plugin properly from there as the plugin is not a node middleware. Anything more to explain ? – Istiaque Ahmed Jan 13 '15 at 16:58
  • Hi Istiaque. You had requested that I take a look at this on one of my other React answers. I am sorry but I am not able to help here. I have not used React with Node.js or React-router. Sorry. – chantastic Jan 14 '15 at 04:59
  • Though I have great experience with React I don't have any with react-router or react-async. Anyhow, its seems a bit odd that you define the routing _inside_ a components `render` function (App.jsx). – Hulvej May 25 '15 at 09:06

1 Answers1

4

I cannot understand your question description, but this answers the title question:

How you render React components outside of react?

MyComponent = require('MyComponent')

element = document.getElementById('postList');

renderedComp = ReactDOM.render(MyComponent,{...someProps},element); 

// => render returns the component instance.

$(document).on('something, function(){
   renderedComp.setState({thingClicked: true})
})

Inside of react you can just call the component.

Blair Anderson
  • 17,040
  • 7
  • 64
  • 96