I am upgrading a legacy web2py (python) application to use react components. I am using webpack to transpile the jsx files to minified js bundle. I want to be able to use:
ReactDOM.render(
<ComponentA arg1="hello" arg2="world" />,
document.getElementById('react-container')
);
Where ComponentA is included in the bundle and the bundle is included on the web2py view. The issue is that I can't access ComponentA in the view. The following example will work:
<script>
var ComponentA = React.createClass({
render: function() {
var p = React.createElement('p', null, 'Passed these props: ',this.props.arg1, ' and ', this.props.arg2);
var div = React.createElement('div', { className: 'my-test' }, p);
return div;
}
});
var component = React.createElement(ComponentA, {arg1:"hello", arg2:"world"})
ReactDOM.render(
component,//I would rather use <ComponentA arg1="hello" arg2="world" />,
document.getElementById('react-sample')
);
</script>
I looked at exports-loader and webpack-add-module-exports but I have not yet gotten it to work. Any help is greatly appreciated.
I solved it after I came across this StackOverflow answer
First make sure that your
main.jsx
file (which would import all the components) also exports them:Then make sure you use
output.library
("more" info in the docs) in thewebpack.config.js
file:Then in the web2py view (make sure you include the build files e.g. main.js AND the appropriate containers):
NOTE: I decided to use the vanilla react in the view instead of the JSX so no additional transpiling has to be done in the browser.