How to load es6, react, babel code in html with cdn?

前端 未结 4 1611
Happy的楠姐
Happy的楠姐 2020-12-01 09:49

I have Codepen code that I\'m trying to replicate on an web page using just three files, index.html, main.js, and style.css. I tried loading these scripts on the head tag of

4条回答
  •  -上瘾入骨i
    2020-12-01 10:17

    You need to use babel standalone script to transcompile the code, and you need to include the script for react and react-dom, use these it will work:

    
    
    
    

    Reason why it is working with codepen: check the setting/javascript, there you will find the babel is selected as JavaScript Preprocessor, codepen is including the script automatically, but to run these files locally you need to include the standalone script.

    Update:

    1- You need to define the script after the div in which you are rendering the react code, otherwise it will throw the error. like this:

     
       

    2- Use ReactDOM.render instead of React.render.

    Check the working code:

    
    
    
    
    
    
    
    
    
       

提交回复
热议问题