Jamie
Jamie

Reputation: 2081

React using import vs. required

I am currently learning react and I have an app using webpack, babel and react. It seems react has two ways of writing it using either required or import. It also seems there is a lot more documentation on using import. How can I change my stack to use the import version?

Upvotes: 1

Views: 1325

Answers (2)

Win
Win

Reputation: 2133

import is ES6 (or ES2015) standard. To use it, you need to install and activate the preset in babel.

Follow these steps:

  1. Go to your project folder then type this: npm install --save-dev babel-cli babel-preset-env
  2. Create a file named .babelrc (in case you have not created one) and insert this lines:

    { 'presets': ['env', 'react'] }

I assume you have setup the webpack to work with babel.

Upvotes: 0

Nevosis
Nevosis

Reputation: 1415

The import and export statements are an ES6 standard. Right now, your setup is likely using Babel to transpile this into ES5. You can use one or the other, but import/export will soon become the standard, so adopting it is advised.

Upvotes: 5

Related Questions