react icon indicating copy to clipboard operation
react copied to clipboard

External JSX files are not executed synchronously

Open ickata opened this issue 10 years ago • 17 comments

I have the following code:

<script type="text/babel" src="/JSX/App.jsx"></script>
<script type="text/babel">
ReactDOM.render( <App model={'/data'}/>, document.body );
</script>

An it is throwing an error that App component is not defined. It seems that the inline script below is executed before the external App.jsx file is loaded.

This, however, used to work in 0.13.*:

<script type="text/jsx" src="/JSX/App.jsx"></script>
<script type="text/jsx">
React.render( <App model={'/data'}/>, document.body );
</script>

ickata avatar Jan 01 '16 19:01 ickata

@ickata You use Babel in the first example. What do you use in the 2nd example to transpile the JSX? Both are at least different, isn't that an explanation for your problem?

rickbeerendonk avatar Jan 02 '16 17:01 rickbeerendonk

In the second example I use JSXTransformer, which was distributed with React 0.13.* and it was also stated in the official documentation that you need to include it in your source code in order to transpile your JSX markup.

Now, with version 0.14.* this JSXTransformer is NOT distributed in official download page, and moreover in the official documentation it is stated that I should use Babel to transpile the JSX markup. Not any trace of the old JSXTransformer.

Does this answer your question, @rickbeerendonk ?

ickata avatar Jan 02 '16 17:01 ickata

@ickata It does. So you use two different tools, so different behavior might be caused by the difference between the tools.

Render directly to document.body is not recommended. Are your scripts part of the body?

When is the JSX transpiled into JS?

rickbeerendonk avatar Jan 02 '16 18:01 rickbeerendonk

Look, @rickbeerendonk – I am using the tool specified in the documentation. If there are more than one tool for JSX transpile – you guys better list them in the official documentation, with all pros & cons and differences.

The scripts are part of the body, but the behavior is the same as if I use a dedicated container for rendering (I used document.body for simplicity here).

I guess the JSX is transpiled onDOMReady. Here's my HTML:

<!DOCTYPE html>
<html>
   <head>
      <title>Hello, React</title>
      <link rel="stylesheet" type="text/css" href="css/style.css">
   </head>
   <body>
      <div id="app"></div>

      <script type="text/javascript" src="/Scripts/react.js"></script>
      <script type="text/javascript" src="/Scripts/react-dom.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>
      <script type="text/babel" src="/JSX/app.jsx"></script>
      <script type="text/babel">
      ReactDOM.render( <App model={'/data'}/>, document.getElementById( 'app' ) );
      </script>
   </body>
</html>

ickata avatar Jan 02 '16 18:01 ickata

Hi @ickata, I will try to reproduce it. Just like you, I've only used the Babel and JSX transpilers as a consumer, so I am not aware of the internals and differences. But I hope I can help you with this.

BTW I am not part of the React team nor do I work for Facebook.

rickbeerendonk avatar Jan 02 '16 18:01 rickbeerendonk

Oh, sorry – I thought that you're part of the dev team ;)

ickata avatar Jan 02 '16 19:01 ickata

This is an issue with babel. You can report it here.

@ickata you can identify core members by the "Collaborator" or "Owner" label in the top right corner of their message. image

brigand avatar Jan 02 '16 23:01 brigand

Babel has removed their support for the browser version recently so I doubt they'll support it (unless they changed their mind), but you can try. They currently recommend https://github.com/Daniel15/babel-standalone though it doesn't do the script tag stuff that you're looking for, which is why #5497 exists here. There isn't anything for us to do outside of that linked issue so I'm going to close out. Sorry you had issues.

My other recommendation is really not to rely on the in-browser transform for anything beyond a quick prototype. It's handy for that but anything more serious will require a build step so you aren't sending 1.8MB of babel's JS to then transform and run again.

zpao avatar Jan 03 '16 00:01 zpao

FWIW, I think it's handled correctly by my unofficial/unsupported babel6-browser transform: http://www.jimsproch.com/react/

That's what I use myself when testing locally, so I thought I'd share.

jimfb avatar Jan 13 '16 14:01 jimfb

@jimfb – no, that also does not work.

ickata avatar Jan 15 '16 08:01 ickata

@ickata Appears to work for me: http://goo.gl/tQUx6V

Do you have a demo that shows otherwise?

jimfb avatar Jan 15 '16 11:01 jimfb

@ickata BTW I had made tool which you can use to proptotype with JSX. Just include the JSX files as normal scripts (doesn't support inline scripts though). - https://github.com/satya164/quik

satya164 avatar Jan 15 '16 16:01 satya164

@jimfb – there you go https://github.com/ickata/react-example

@satya164 – unfortunately I need something for both inline and external JSX scripts (and I need them to be executed in order)

ickata avatar Jan 18 '16 09:01 ickata

@ickata The execution order is correct. The problem there is scoping.

Use window.App = React.createClass({... instead of var App = React.createClass({... to indicate that you want the variable to be accessible in the global scope (ie. outside the local file). Let me know if that doesn't work.

jimfb avatar Jan 18 '16 19:01 jimfb

@jimfb - I think that used to work. vars like that are accessible outside the file (unless they're protected inside a function) so I think something isn't right with your script. Do you have the original source? Or is this just straight babel-browser?

zpao avatar Jan 18 '16 19:01 zpao

Tested quickly - execution order is correct but the transformed script isn't being run in the "right" (read: expected) scope, which is why this !== window and why var App isn't available outside of that file boundary. If you're going to suggest that to anybody, you should probably fix it (or just work on #5497 and then we can review it since we're probably going to support this in some way for the time being).

zpao avatar Jan 18 '16 19:01 zpao

@ickata Looks like this scoping issue is fixed, let me know if you run into any further issues.

jimfb avatar Mar 01 '16 10:03 jimfb