{"id":1742,"date":"2016-03-18T18:21:23","date_gmt":"2016-03-18T12:51:23","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=1742"},"modified":"2016-03-18T18:21:23","modified_gmt":"2016-03-18T12:51:23","slug":"using-reactjs-with-babel-and-es6-2","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/using-reactjs-with-babel-and-es6-2\/","title":{"rendered":"Using ReactJS with Babel and ES6"},"content":{"rendered":"<p>I wanted to create this post\u00a0shortly\u00a0after the last post about <a href=\"http:\/\/ramkulkarni.com\/blog\/setting-up-es6-babel-gulp\/\">Setting up ES6+Babel+Gulp<\/a>, but it got delayed for some reasons. Anyways ..<\/p>\n<p>Though it is not very complicated to setup ReactJS to work with Babel, there are a few non-obvious things you need to be aware of, specifically the absence of \u00a0auto-binding in ReactJS when\u00a0coded in ES6. Let&#8217;s create a small NodeJS project using Gulp &#8211;<\/p>\n<p>Install following packages &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #005fd2;\">npm<\/span> <span style=\"color: #1060b6;\">install<\/span> <span style=\"color: #1060b6;\">gulp<\/span> <span style=\"color: #1060b6;\">gulp-connect<\/span> <span style=\"color: #1060b6;\">vinyl-source-stream<\/span> <span style=\"color: #1060b6;\">browserify<\/span> <span style=\"color: #1060b6;\">babelify<\/span> <span style=\"color: #1060b6;\">babel-preset-es2015<\/span> <span style=\"color: #1060b6;\">react<\/span> <span style=\"color: #1060b6;\">react-dom<\/span> <span style=\"color: #1060b6;\">babel-preset-react<\/span> <span style=\"color: #1060b6;\">--save-dev<\/span>\n<\/pre>\n<p>In addition to packages installed in the last post, here we are installing react, react-dom and\u00a0babel-preset-react packages.<\/p>\n<p>The directory structure is similar to one in the <a href=\"http:\/\/ramkulkarni.com\/blog\/setting-up-es6-babel-gulp\/\">previous post<\/a>, except that we add views folder for JSX files.<\/p>\n<pre style=\"background-color: #d2d2d2;\">ES6ReactJSBabelTest\n  -- build\n  -- src\n    -- js\n       -- views\n    -- html\n  -- gulpfile.js<\/pre>\n<p>Gulp &#8216;build&#8217; task is modified to add &#8216;react&#8217; preset to babelify and add jsx extension to browserify (we want browserify to process .jsx files in addition to .js files)<!--more--><\/p>\n<p><strong>gulpfile.js<\/strong> (partial)<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">gulp<span style=\"color: #308080;\">.<\/span>task<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">build<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #200080; font-weight: bold;\">function<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">return<\/span> browserify<span style=\"color: #308080;\">(<\/span><span style=\"color: #406080;\">{<\/span>\n        entries<span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">[<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">.\/src\/js\/index.js<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">]<\/span><span style=\"color: #308080;\">,<\/span>\n        extensions<span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">[<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">.js<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">.jsx<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">]<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #308080;\">.<\/span>transform<span style=\"color: #308080;\">(<\/span>babelify<span style=\"color: #308080;\">.<\/span>configure<span style=\"color: #308080;\">(<\/span><span style=\"color: #406080;\">{<\/span>\n        presets <span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">[<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">es2015<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">react<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">]<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #308080;\">.<\/span>bundle<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #308080;\">.<\/span>pipe<span style=\"color: #308080;\">(<\/span><span style=\"color: #007d45;\">source<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">bundle.js<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #308080;\">.<\/span>pipe<span style=\"color: #308080;\">(<\/span>gulp<span style=\"color: #308080;\">.<\/span>dest<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">.\/build<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<\/pre>\n<p>We will then\u00a0create index.html in html folder with following content.<\/p>\n<p><strong>html\/index.html<\/strong><\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #004a43;\">&lt;!DOCTYLE html&gt;<\/span>\n\n<span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #200080; font-weight: bold;\">html<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n    <span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #200080; font-weight: bold;\">head<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n        <span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #200080; font-weight: bold;\">title<\/span><span style=\"color: #0057a6;\">&gt;<\/span>Babel + ReactJS Test<span style=\"color: #0057a6;\">&lt;\/<\/span><span style=\"color: #200080; font-weight: bold;\">title<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n    <span style=\"color: #0057a6;\">&lt;\/<\/span><span style=\"color: #200080; font-weight: bold;\">head<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n    <span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #200080; font-weight: bold;\">body<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n        <span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #200080; font-weight: bold;\">div<\/span> <span style=\"color: #074726;\">id<\/span><span style=\"color: #308080;\">=<\/span><span style=\"color: #1060b6;\">\"main\"<\/span><span style=\"color: #0057a6;\">\/&gt;<\/span>\n        <span style=\"color: #0057a6;\">&lt;<\/span><span style=\"color: #200080; font-weight: bold;\">script<\/span> <span style=\"color: #074726;\">src<\/span><span style=\"color: #308080;\">=<\/span><span style=\"color: #1060b6;\">\"bundle.js\"<\/span><span style=\"color: #0057a6;\">&gt;<\/span><span style=\"color: #0057a6;\">&lt;\/<\/span><span style=\"color: #200080; font-weight: bold;\">script<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n    <span style=\"color: #0057a6;\">&lt;\/<\/span><span style=\"color: #200080; font-weight: bold;\">body<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n<span style=\"color: #0057a6;\">&lt;\/<\/span><span style=\"color: #200080; font-weight: bold;\">html<\/span><span style=\"color: #0057a6;\">&gt;<\/span>\n<\/pre>\n<p>We will embed a ReactJS component in the main div above. Now we will create index.js in js folder which will create a ReactJS component and add it to the main div.<\/p>\n<p><strong>js\/index.js<\/strong><\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">import<\/span> React from <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">react<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #200080; font-weight: bold;\">import<\/span> ReactDOM from <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">react-dom<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #200080; font-weight: bold;\">import<\/span> MainView from <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">.\/views\/MainView<\/span><span style=\"color: #800000;\">\"<\/span>\n\n<span style=\"color: #200080; font-weight: bold;\">var<\/span> mainView <span style=\"color: #308080;\">=<\/span> React<span style=\"color: #308080;\">.<\/span>createElement<span style=\"color: #308080;\">(<\/span>MainView<span style=\"color: #308080;\">,<\/span><span style=\"color: #406080;\">{<\/span>name<span style=\"color: #406080;\">:<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">Stranger<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #0f4d75;\">null<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\nReactDOM<span style=\"color: #308080;\">.<\/span>render<span style=\"color: #308080;\">(<\/span>mainView<span style=\"color: #308080;\">,<\/span> document<span style=\"color: #308080;\">.<\/span>getElementById<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">main<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<\/pre>\n<p>Note that MainView is passed name attribute with &#8220;Stranger&#8221; value. Let&#8217;s now create the ReactJS Component, MainView in views folder.<\/p>\n<p><strong>views\/MainView.jsx<\/strong><\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">import<\/span> React from <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">react<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #406080;\">;<\/span>\n\n<span style=\"color: #200080; font-weight: bold;\">export<\/span> <span style=\"color: #200080; font-weight: bold;\">default<\/span> <span style=\"color: #200080; font-weight: bold;\">class<\/span> MainView <span style=\"color: #200080; font-weight: bold;\">extends<\/span> React<span style=\"color: #308080;\">.<\/span>Component <span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #007d45;\">constructor<\/span> <span style=\"color: #308080;\">(<\/span>props<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">super<\/span><span style=\"color: #308080;\">(<\/span>props<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>state <span style=\"color: #308080;\">=<\/span> <span style=\"color: #406080;\">{<\/span>\n            name <span style=\"color: #406080;\">:<\/span> props<span style=\"color: #308080;\">.<\/span>name\n        <span style=\"color: #406080;\">}<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    onSetName<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>setState<span style=\"color: #308080;\">(<\/span><span style=\"color: #406080;\">{<\/span>\n            name <span style=\"color: #406080;\">:<\/span> <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>refs<span style=\"color: #308080;\">.<\/span>nameTxt<span style=\"color: #308080;\">.<\/span>value\n        <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    render<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #308080;\">&lt;<\/span>div<span style=\"color: #308080;\">&gt;<\/span>\n            Hello <span style=\"color: #406080;\">{<\/span><span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>state<span style=\"color: #308080;\">.<\/span>name<span style=\"color: #406080;\">}<\/span> <span style=\"color: #308080;\">!<\/span>\n            <span style=\"color: #308080;\">&lt;<\/span>br<span style=\"color: #308080;\">\/<\/span><span style=\"color: #308080;\">&gt;<\/span>\n            Enter Name <span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">&lt;<\/span><span style=\"color: #007d45;\">input<\/span> type<span style=\"color: #308080;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">text<\/span><span style=\"color: #800000;\">\"<\/span> ref<span style=\"color: #308080;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">nameTxt<\/span><span style=\"color: #800000;\">\"<\/span> <span style=\"color: #308080;\">\/<\/span><span style=\"color: #308080;\">&gt;<\/span>\n            <span style=\"color: #308080;\">&lt;<\/span>button type<span style=\"color: #308080;\">=<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">button<\/span><span style=\"color: #800000;\">\"<\/span> onClick<span style=\"color: #308080;\">=<\/span><span style=\"color: #406080;\">{<\/span><span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>onSetName<span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">&gt;<\/span>Set Name<span style=\"color: #308080;\">&lt;<\/span><span style=\"color: #308080;\">\/<\/span>button<span style=\"color: #308080;\">&gt;<\/span>\n        <span style=\"color: #308080;\">&lt;<\/span><span style=\"color: #308080;\">\/<\/span>div<span style=\"color: #308080;\">&gt;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n<span style=\"color: #406080;\">}<\/span>\n<\/pre>\n<p>Note that when you write ReactJS component in ES6, you do not override setState method, &#8211; you\u00a0create this.state object in the constructor, if you want to initialize the component state. In the above example, we add name to state and initialize it with\u00a0the name attribute\/property passed to the component. Recall that in\u00a0the index.js, we have passed\u00a0name=&#8221;Stranger&#8221; to the MainView component.<\/p>\n<p>In the render method we create a greeting string, using name stored in the state and also create a text box and a button to set name. onClick handler of the button is set to onSetName method of the component. So if you type something in the text box and click Set Name button then you would expect greeting to change with the content of the text box. However if you run this example (run gulp and browse to http:\/\/localhost:9001\/index.html) and try to set name, you will see that greeting is not updated with the name. In the JS console you would see following error :<\/p>\n<p><span style=\"color: red;\">Uncaught TypeError: Cannot read property &#8216;setState&#8217; of null<\/span><\/p>\n<p>The reason is that\u00a0ReactJS component is compiled to a new JS object with method names as keys and method body as values &#8211; here the code generated for our MainView component :<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">var<\/span> MainView <span style=\"color: #308080;\">=<\/span> <span style=\"color: #200080; font-weight: bold;\">function<\/span> <span style=\"color: #308080;\">(<\/span>_React$Component<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n    _inherits<span style=\"color: #308080;\">(<\/span>MainView<span style=\"color: #308080;\">,<\/span> _React$Component<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\n    <span style=\"color: #200080; font-weight: bold;\">function<\/span> MainView<span style=\"color: #308080;\">(<\/span>props<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        _classCallCheck<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">,<\/span> MainView<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\n        <span style=\"color: #200080; font-weight: bold;\">var<\/span> _this <span style=\"color: #308080;\">=<\/span> _possibleConstructorReturn<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #007d45;\">Object<\/span><span style=\"color: #308080;\">.<\/span>getPrototypeOf<span style=\"color: #308080;\">(<\/span>MainView<span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">.<\/span>call<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">,<\/span> props<span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\n        _this<span style=\"color: #308080;\">.<\/span>state <span style=\"color: #308080;\">=<\/span> <span style=\"color: #406080;\">{<\/span>\n            name<span style=\"color: #406080;\">:<\/span> props<span style=\"color: #308080;\">.<\/span>name\n        <span style=\"color: #406080;\">}<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> _this<span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    _createClass<span style=\"color: #308080;\">(<\/span>MainView<span style=\"color: #308080;\">,<\/span> <span style=\"color: #308080;\">[<\/span><span style=\"color: #406080;\">{<\/span>\n        key<span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">onSetName<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span>\n        value<span style=\"color: #406080;\">:<\/span> <span style=\"color: #200080; font-weight: bold;\">function<\/span> onSetName<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>setState<span style=\"color: #308080;\">(<\/span><span style=\"color: #406080;\">{<\/span>\n                name<span style=\"color: #406080;\">:<\/span> <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>refs<span style=\"color: #308080;\">.<\/span>nameTxt<span style=\"color: #308080;\">.<\/span>value\n            <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #406080;\">{<\/span>\n        key<span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">render<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span>\n        value<span style=\"color: #406080;\">:<\/span> <span style=\"color: #200080; font-weight: bold;\">function<\/span> render<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">return<\/span> _react2<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">default<\/span><span style=\"color: #308080;\">.<\/span>createElement<span style=\"color: #308080;\">(<\/span>\n                <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">div<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span>\n                <span style=\"color: #0f4d75;\">null<\/span><span style=\"color: #308080;\">,<\/span>\n                <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">Hello <\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span>\n                <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>state<span style=\"color: #308080;\">.<\/span>name<span style=\"color: #308080;\">,<\/span>\n                <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\"> !<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span>\n                _react2<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">default<\/span><span style=\"color: #308080;\">.<\/span>createElement<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">br<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #0f4d75;\">null<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">,<\/span>\n                <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">Enter Name : <\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span>\n                _react2<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">default<\/span><span style=\"color: #308080;\">.<\/span>createElement<span style=\"color: #308080;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">input<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #406080;\">{<\/span> type<span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">text<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span> ref<span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">nameTxt<\/span><span style=\"color: #800000;\">\"<\/span> <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">,<\/span>\n                _react2<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">default<\/span><span style=\"color: #308080;\">.<\/span>createElement<span style=\"color: #308080;\">(<\/span>\n                    <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">button<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span>\n                    <span style=\"color: #406080;\">{<\/span> type<span style=\"color: #406080;\">:<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">button<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span> onClick<span style=\"color: #406080;\">:<\/span> <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>onSetName <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">,<\/span>\n                    <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">Set Name<\/span><span style=\"color: #800000;\">\"<\/span>\n                <span style=\"color: #308080;\">)<\/span>\n            <span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">]<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\n    <span style=\"color: #200080; font-weight: bold;\">return<\/span> MainView<span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">(<\/span>_react2<span style=\"color: #308080;\">.<\/span><span style=\"color: #200080; font-weight: bold;\">default<\/span><span style=\"color: #308080;\">.<\/span>Component<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<\/pre>\n<p>As you can see, setName method becomes value of a field setName in the object passed to _createClass method. &#8216;this&#8217; scope in this event handler method is n0t the same as &#8216;this&#8217; scope for the component. If you write ReactJS code in ES5, then the compiler does auto-binding of user defined component methods to the component, but it does not do so when the code is written in ES6. So we need to manually do this auto-binding. One way to do this is to specifically bind user defined methods to the component in the constructor &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #007d45;\">constructor<\/span> <span style=\"color: #308080;\">(<\/span>props<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">super<\/span><span style=\"color: #308080;\">(<\/span>props<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>state <span style=\"color: #308080;\">=<\/span> <span style=\"color: #406080;\">{<\/span>\n            name <span style=\"color: #406080;\">:<\/span> props<span style=\"color: #308080;\">.<\/span>name\n        <span style=\"color: #406080;\">}<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>onSetName <span style=\"color: #308080;\">=<\/span> <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>onSetName<span style=\"color: #308080;\">.<\/span>bind<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n<\/pre>\n<p>But this is not a very good solution, because you will have to do this for all user defined functions in your component.<\/p>\n<p>Other solution is to use <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/JavaScript\/Reference\/Functions\/Arrow_functions\" target=\"_blank\">fat arrow, =&gt;, functions\u00a0<\/a>of ES6. When you declare function using fat arrow, the function does not create its own &#8216;this&#8217; scope, but uses the active &#8216;this&#8217; scope at that point. So we could write setName event handler in our component as &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">onSetName <span style=\"color: #308080;\">=<\/span> <span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #308080;\">=<\/span><span style=\"color: #308080;\">&gt;<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>setState<span style=\"color: #308080;\">(<\/span><span style=\"color: #406080;\">{<\/span>\n            name <span style=\"color: #406080;\">:<\/span> <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>refs<span style=\"color: #308080;\">.<\/span>nameTxt<span style=\"color: #308080;\">.<\/span>value\n        <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n<\/pre>\n<p>But this code won&#8217;t compile in ES6 (try gulp command to build the application), because ES6 does not support property assignment in classes. This is supported in ES7. The good news is that there is a Babel preset (<a href=\"https:\/\/babeljs.io\/docs\/plugins\/preset-stage-0\/\" target=\"_blank\">Stage-0 preset<\/a>) that can translate this code to ES5. So install the preset<\/p>\n<pre><code class=\"language-sh\" data-lang=\"sh\">npm install babel-preset-stage-0 --save-dev\n<\/code><\/pre>\n<p>You also need to add this preset to babelify in the build task to gulpfile.js<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #308080;\">.<\/span>transform<span style=\"color: #308080;\">(<\/span>babelify<span style=\"color: #308080;\">.<\/span>configure<span style=\"color: #308080;\">(<\/span><span style=\"color: #406080;\">{<\/span>\n        presets <span style=\"color: #406080;\">:<\/span> <span style=\"color: #308080;\">[<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">es2015<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">react<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">,<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #1060b6;\">stage-0<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #308080;\">]<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span>\n<\/pre>\n<p>Remember to remove binding code we added in the constructor, if you are going to use the above Babel preset.<\/p>\n<p>With stage-0 preset added, our code should compile and run as expected.<\/p>\n<p>-Ram Kulkarni<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to create this post\u00a0shortly\u00a0after the last post about Setting up ES6+Babel+Gulp, but it got delayed for some reasons. Anyways .. Though it is not very complicated to setup ReactJS to work with Babel, there are a few non-obvious things you need to be aware of, specifically the absence of \u00a0auto-binding in ReactJS when\u00a0coded &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/using-reactjs-with-babel-and-es6-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using ReactJS with Babel and ES6&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[20,1],"tags":[123,127,124,5,128],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-s6","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/1742"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=1742"}],"version-history":[{"count":0,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/1742\/revisions"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=1742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=1742"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=1742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}