未加星标

Polish Up your JavaScript #02: Adding React

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏
In series so far: Setting up development project with Webpack Adding React In the last episode…

In the previous post we have set up working development environment with Babel 7 and Webpack 4. Look there if you haven’t set it up just yet, and download our example repository for code examples.


javascript-02-adding-react/bialowieza.jpg?imageView2/2/h/300/interlace/1" alt="Polish Up your JavaScript #02: Adding React" onerror="imgError(this);"/>
Random awesome Polish thing #02: Biaowiea Forest (photo credit: Freeimages.com/michalski) Install your NPM packages

You will need react and react-dom packages, plus an extra preset for Babel 7, to make it understand React templating (JSX).

$ npm install --save react react-dom $ npm install --save-dev @babel/preset-react

Edit your .babelrc to instruct Babel to use the above preset when transpiling your src/index.js and any other source files:

{ "presets": ["@babel/preset-env", "@babel/preset-react"] }

We need additional because React uses special syntax, which looks like HTML tags embedded into JavaScript. In order to expand these to appropriate function calls, @babel/preset-react is needed.

React component

Open up your src/index.js file, and change it slightly so we append an empty div controller to our web site’s body element. This will be our starting point of the application, and place where we mount the top-level React component:

... const reactRoot = document.createElement("div"); reactRoot.setAttribute("id", "react-root"); document.body.appendChild(reactRoot);

This should add the empty div to the HTML dynamically. Check it out in development tool by right-clicking on the body of the page and clicking “Inspect element”.

We also need to import React and ReactDOM at the very top of the file:

import "./index.scss" import ReactDOM from "react-dom"; import React from "react"; ...

And finally, we need to write our component and render it.

const App = function(props) { return(<div>Hello, React!</div>); } ReactDOM.render(<App />, reactRoot);

Our component is called App and you make it render within our reactRoot by executing ReactDOM.render function.

The <App /> tag will be transpiled to simple function call. In our example we could also get away with:

const App = function(props) { return(<div>Hello, React!</div>); } ReactDOM.render(App(), reactRoot);

and the result would be precisely the same, but the first syntax is preferred.

As we can see, our React component is a simple function, which returns some HTML elements, which will be inserted into our container ( reactRoot ) when we execute this function.

Since we are writing ES6+ JavaScript, we will have a shorter function syntax on hand. It’s called Arrow functions , and there are slight differences between using function() and arrow function syntax - but we can ignore them for now as they are irrelevant for our example. We will explore arrow functions in details in next part.

The arrow function syntax for our component would be:

const App = (props) => <div>Hello, React!</div>; ReactDOM.render(<App />, reactRoot);

We will not explore class based components in this post, but we will do it shortly after. Class-based components are currently still needed on occassion for most React applications, but my strong prediction is they will become thing of the past very soon.

You should write function-based components as a default way of writing your code.

For the sake of experimenting, let’s try turning our function-based component into class-based:

class App extends React.Component { render() { return <div>Hello, class-based React!</div>; } } ReactDOM.render(<App />, reactRoot); Sources for this blog post

You can find the project’s source files for this blog post on our GitHub under https://github.com/amberbit/polish-up-your-javascript/tree/master/polish-up-your-javascript-1

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:Polish Up your JavaScript #02: Adding React
本站链接:https://www.codesec.net/view/620735.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(10)