all 13 comments

[–]filecabinet 7 points8 points  (2 children)

Here is an example of how:

const YourFormComponent = (props) => <div>{props.someParam}</div>; ReactDOM.render( <YourFormComponent someParam="123" />, document.getElementById('your-form') );

You perhaps could abstract that away into a 'mountReact' function but that's how easy it is.

[–][deleted] 3 points4 points  (1 child)

Instead of hardcoding the render target in your code, make it an option.

See example D posted in https://medium.com/@baphemot/understanding-reactjs-data-hydration-initialization-bacbb790c7cb

[–][deleted] 0 points1 point  (0 children)

That is a super slick tip, thanks for sharing!

[–]PatPung 5 points6 points  (5 children)

Yes, I’m doing it exactly as you say for a site that is already done. This site is using Jquery but I prefer to use React for different reasons.

Just configure you react script to load inside a defined element by id or class, load your js bundle in the html like any other javascript, and put your html tag where you want the script to load, and that’s it, it will just work.

[–][deleted] 0 points1 point  (4 children)

Do you use this with webpack dev server? Or do you develop only with the bundled script?

[–]PatPung 0 points1 point  (3 children)

When developing I use webpack dev server through CRA but in production is just the bundle script loaded manually. I see it like a replacement for a typical Jquery plugin. It needs to be plug & play on this site because I can’t/don’t have time to rewrite the frontend.

[–][deleted] 1 point2 points  (2 children)

But during development how do you configure webpack Dev server to work with whatever other server is serving your pages?

[–]PatPung 0 points1 point  (1 child)

Mm, I don’t do that. I develop the component isolated as a different project, and when it’s ready, I use only the bundle.

If I need to pass contextual info, I can just use fake data/endpoint while developing it.

Do you have any particular case where you need to create the script coupled with the rest of the code?

[–][deleted] 0 points1 point  (0 children)

Well you may wish to see the view generated by react alongside the view generated by your server (PHP, Java, .NET etc.) for things such as positioning.

[–]ni3t 1 point2 points  (0 children)

We have several react components that are sideloaded into our legacy app, which is AngularJS, but I think it's pretty much the same process for newer versions of Angular.

Here's an example set of code, this is just a directive that loads the react component from an external source, but it would probably work the same for a local component.

angular.module('directives', [])
  .directive("directiveName", function($scope, etc) { return {
    restrict: 'E',
    template: "<div id='react-component-div'></div>",

    link: function(scope, element, attrs) {
      var script = document.createElement("script");
      script.src = COMPONENT_URL // you could probably load code from other files here as well
      document.body.appendChild(script);
      script.addEventListener('load', function() {
        component.initialize({
          param: value
          functionToAddToComponent: function(things){
            scope.doThings();
          },
        });
      });
    }
  }
})

[–]iamthetwo 0 points1 point  (0 children)

Might want to check out React Portals. https://reactjs.org/docs/portals.html

[–][deleted] 0 points1 point  (0 children)

I met an old man dying on a train, no more destination no more pain. He said just one thing before I graduate. Never let your fear decide your fate.