javascript - Splitting up React components into multiple files -


i have been learning react in week new , trying write simple todo app.

originally wrote of components in 1 file , loaded file html file , worked great. refactoring , trying split components different files.

my full code on github https://github.com/yasgreen93/todolist-react on extracting-files branch.

i have split each component different files , have linked them in script tags html. html file looks like:

<!doctype html> <html>   <head>     <meta charset="utf-8">     <title>todo list</title>     <link rel="stylesheet" href="css/styles.css">     <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.1/react-dom.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.16/browser.js"></script>     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>   </head>    <body>     <div id="container"></div>     <script type="text/babel" src="scripts/components/todolistapp.js"> </script>     <script type="text/babel" src="scripts/components/completetodo.js"></script>     <script type="text/babel" src="scripts/components/todolist.js"></script>     <script type="text/babel" src="scripts/components/singletodo.js"></script>     <script type="text/babel" src="scripts/components/addtodo.js"></script>     <script type="text/babel" src="scripts/components/completetodobutton.js"></script>      <script type="text/babel">       reactdom.render(         <todolistapp url="/api/todos" updateurl="/api/todos/update" pollinterval={2000}/>,         document.getelementbyid('container')       );     </script>   </body> </html> 

in console, error message uncaught referenceerror: todolistapp not defined. have tried loading them in different orders no success. have watched many videos similar approaches without using webpack , works them. working first without using webpack , move on learning that.

can see going wrong?

you have add components global window variable in order use them in html script tag. window.todolistapp =.... var declaration relative file in declare it.

but considered bad practice expose parts of code global scope , transpile jsx in browser. instead should consider use building system webpack.

this way able use es2015 import syntax import components 1 file another, bundle in 1 file , more additional benefits code minification, sourcemaps, livereload etc.

setting react es6 webpack , babel

using react webpack tutorial


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -