{"id":741,"date":"2022-02-24T14:27:48","date_gmt":"2022-02-24T19:27:48","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=741"},"modified":"2024-02-09T14:43:40","modified_gmt":"2024-02-09T19:43:40","slug":"quick-introduction-to-react-js","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/quick-introduction-to-react-js\/","title":{"rendered":"Quick Introduction to React.js"},"content":{"rendered":"\n<p>React is an open source JavaScript library created by Facebook and maintained by a large community. It is used for creating views as in V in MVC. It is a great library for creating large-scale, single page applications.<\/p>\n\n\n\n<p>React is very fast, primarily because it uses a virtual DOM. Reading and writing to DOM is much more expensive than reading or writing to JavaScript objects. Virtual DOM are JavaScript objects that only write to DOM when it is absolutely necessary.<\/p>\n\n\n\n<p>React supports JSX syntax (JavaScript as XML).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>HTML5<\/li><li>CSS3<\/li><li>JavaScript<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Environment setup for React<\/h3>\n\n\n\n<p>All you need to do to start building with React is include react.js and react-dom.js inside the &lt;head&gt; tag.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;head&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react-dom.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n<\/code><\/pre>\n\n\n\n<p>For debugging, add the following chrome extensions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>React detector<\/li><li>React developer tool<\/li><\/ul>\n\n\n\n<p>To test, go to airbnb.ca and then click on the react detector symbol on top right of your browser.<\/p>\n\n\n\n<p>Note: (cmd+opt+j) or (ctrl+shift+j) are shortcut for developer tools.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">First Program<\/h3>\n\n\n\n<p>Simply copy paste the following code in a file called demo1.php and open it with a browser.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react-dom.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/babel-core\/5.8.34\/browser.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div id='container1'&gt;&lt;\/div&gt;\n    &lt;script&gt;\n        ReactDOM.render(\n            React.createElement('div', null, 'Hello World'),\n            document.getElementById('container1')\n        )\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>The code is simply creating a div element with Hello World string, assigning it to container1 and rendering it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Component<\/h3>\n\n\n\n<p>Components are small user-interface elements that display content. It could be a form, table, or anything else. In the following example, we create a component that displays text.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react-dom.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/babel-core\/5.8.34\/browser.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div id='container1'&gt;&lt;\/div&gt;\n    &lt;script type=\"text\/babel\"&gt;\n         var MyComponent = React.createClass({\n            render() {\n                return &lt;div&gt;&lt;h1&gt;Hello World&lt;\/h1&gt;&lt;\/div&gt;\n            }\n        })\n\n        ReactDOM.render(&lt;MyComponent \/&gt;, document.getElementById('container1'))\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Component Properties<\/strong>&nbsp;Following example show how to use component properties<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react-dom.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/babel-core\/5.8.34\/browser.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div id='container1'&gt;&lt;\/div&gt;\n    &lt;script type=\"text\/babel\"&gt;\n    var PrintComponent = React.createClass ({\n        render() {\n            return &lt;div&gt;\n                &lt;h3&gt;{this.props.title}&lt;\/h3&gt;\n                &lt;p&gt;{this.props.children}&lt;\/p&gt;\n                &lt;\/div&gt;\n        }\n    })\n\n    ReactDOM.render(&lt;div&gt;\n        &lt;PrintComponent title=\"Title1\"&gt;Message 1&lt;\/PrintComponent&gt;\n        &lt;PrintComponent title=\"Title2\"&gt;Message 2&lt;\/PrintComponent&gt;\n        &lt;\/div&gt;, \n        document.getElementById('container1'))\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>Note that we used PrintComponent element twice. It can be used as many times as you like. Also note that this.props.title and this.props.children are taking values from the PrintComponent elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Events<\/h3>\n\n\n\n<p>In the following example, we create three functions which respond to onClick event.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n&lt;head&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/15.1.0\/react-dom.js\"&gt;&lt;\/script&gt;\n    &lt;script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/babel-core\/5.8.34\/browser.min.js\"&gt;&lt;\/script&gt;\n    &lt;link rel=\"stylesheet\" href=\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.3.7\/css\/bootstrap.min.css\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;div id='container1'&gt;&lt;\/div&gt;\n    &lt;script type=\"text\/babel\"&gt;\n    var PrintComponent = React.createClass ({\n        add() {\n            alert('add a message');\n        }, \n        edit() {\n            alert('edit this message');\n        }, \n        remove() {\n            alert('delete this message');\n        },\n        render() {\n            return &lt;div&gt;\n                &lt;button onClick={this.add}&gt;Add Message&lt;\/button&gt;\n                &lt;span&gt;\n                    {this.props.children}\n                    &lt;button onClick={this.edit}&gt;Edit&lt;\/button&gt;\n                    &lt;button onClick={this.remove}&gt;Delete&lt;\/button&gt;\n                &lt;\/span&gt;\n                &lt;\/div&gt;\n        }\n    })\n\n    ReactDOM.render(&lt;div&gt;\n        &lt;PrintComponent&gt;Message 1&lt;\/PrintComponent&gt;\n        &lt;PrintComponent&gt;Message 2&lt;\/PrintComponent&gt;\n        &lt;\/div&gt;, \n        document.getElementById('container1'))\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>The alert() functions simply serve to validate that the program responded to an onClick event.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is an open source JavaScript library created by Facebook and maintained by a large community. It is used for creating views as in V in MVC. It is a great library for creating large-scale, single page applications. React is very fast, primarily because it uses a virtual DOM. Reading and writing to DOM is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[185],"tags":[54],"class_list":["post-741","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=741"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/741\/revisions"}],"predecessor-version":[{"id":742,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/741\/revisions\/742"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}