Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use react require syntax?

Tags:

I see lots of examples for react structured with var React = require('react') syntax. When I try and use this I get "require is not defined". How do I use and set up my static project to use require syntax?

Update

In actuality I am looking for a webpack/browserify config file so I can get started with React and CommonJS quickly. I have only written react apps without said build tools

like image 624
Connor Leech Avatar asked Oct 20 '15 22:10

Connor Leech


People also ask

Can you use require in React?

While you can still use require() and module. exports , we encourage you to use import and export instead.

Why we use require in React?

require comes from commonjs and is most famously implemented in node. js, if you have used node. js, you will see requires everywhere. due to the popularity of require in node, people have built tools which will transform code that is written in nodejs style to be useable on the browser.

What is require () in JavaScript?

1) require() require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.


2 Answers

require is not a React api, nor is it a native browser api (for now).

require comes from commonjs and is most famously implemented in node.js, if you have used node.js, you will see requires everywhere.

due to the popularity of require in node, people have built tools which will transform code that is written in nodejs style to be useable on the browser.

there's a few benefits to using require, it helps you keep your code modular and for some projects it allows you to write isomorphic code (code that runs both on client and server with minimal change)

In order to use require, you will need to use a tool such as webpack or browserify, I will use browserify as an example.

first create an 'index.js'

require('./app.js'); alert('index works'); 

then create an app.js

alert('app works'); 

next install the browserify cli

npm install -g browserify 

And call this command in your shell

browserify index.js > bundle.js 

Now you will have a bundle.js, in your html page create a

<script src="bundle.js"></script> 

And you should see both alerts run

Now you can continue to code, you can add react in your code by doing a

npm install react --save 

and then require it in app.js for example

var React = require('react');  React.createClass({     render: function(){/*Blah Blah Blah*/} }) 
like image 122
yangli-io Avatar answered Oct 03 '22 16:10

yangli-io


BTW, If you are using webpack you can add to your webpack.config.js config file The following lines, eliminating the need to use require statements in your files:

 plugins: [             new webpack.ProvidePlugin({               'React':     'react',               '$':          'jquery',               '_':          'lodash',               'ReactDOM':   'react-dom',               'cssModule':  'react-css-modules',               'Promise':    'bluebird'             })          ], 

This is of course less verbose and not all developers like it, but it's good to know :)

like image 22
danikoren Avatar answered Oct 03 '22 15:10

danikoren