reactjs快速如梦

此帖子分为2部分 (This Post is divided into 2 parts)

  1. The First Part demonstrates how to create a simple React app using ‘create-react-app’ CLI and explains the project structure.第一部分演示了如何使用“ create-react-app” CLI创建简单的React应用,并说明了项目结构。
  2. The Second Part explains an existing code that I have posted in Github. This code demonstrates the use of components, communication between components, making HTTP calls and React Bootstrap (bootstrap which is written for React).第二部分介绍了我在Github中发布的现有代码。 该代码演示了组件的使用,组件之间的通信,进行HTTP调用以及React Bootstrap(为React编写的Bootstrap)。

第1部分 (Part 1)

如果尚未安装NodeJS,请安装 (Install NodeJS if not already present)

NodeJS is needed since the Libraries Required for React are downloaded using node package manager ( npm ). Refer to https://nodejs.org/en/ to install NodeJS.

需要NodeJS,因为使用节点包管理器(npm)下载了React所需的库。 请参阅https://nodejs.org/en/安装NodeJS。

安装create-react-app节点软件包 (Install create-react-app Node Package)

create-react-app node package helps to set up a React project. Install create-react-app node package globally using the following command.

create-react-app节点包有助于建立一个React项目。 使用以下命令全局安装create-react-app节点程序包。

npm install -g create-react-app

创建项目 (Create The Project)

The project can be created using create-react-app. Use the following command to create the project.

可以使用create-react-app创建项目 使用以下命令创建项目。

npx create-react-app first-react-app

first-react-app is the name of the application. The above command creates a folder called first-react-app which is the project folder. In order to test if everything has been set up properly, go into the project folder and start the application using the following command.

first-react-app应用程序的名称。 上面的命令创建一个名为first-react-app的文件夹,它是项目文件夹。 为了测试是否一切都已正确设置,请进入项目文件夹并使用以下命令启动应用程序。

cd first-react-app
npm start

Go to your browser and go the following URL localhost:3000You should be able to see that your application is running. The Application will look like this in your browser:

转到浏览器并转到以下URL localhost:3000,您应该能够看到您的应用程序正在运行。 该应用程序在您的浏览器中将如下所示:

基本文件夹结构介绍 (Basic Folder Structure Explained)

When you created the project, you would have noticed that it created a bunch of files. Here I will list out some of the important files and folders that you should be aware of .

创建项目时,您会注意到它创建了许多文件。 在这里,我将列出您应该注意的一些重要文件和文件夹。

  1. package.json: This File has the list of node dependencies which are needed.

    package.json:此文件包含所需的节点依赖项列表。

  2. public/index.html: When the application starts this is the first page that is loaded. This will be the only html file in the entire application since React is generally Written using JSX which I will cover later. Also, this file has a line of code <div id=”root”></div>. This line is very significant since all the application components are loaded into this div.

    public / index.html:应用程序启动时,这是加载的第一页。 这是整个应用程序中唯一的html文件,因为React通常是使用JSX编写的,我将在稍后介绍。 此外,该文件还有一行代码<div id =” root”> </ div> 。 这条线是非常显著,因为所有的应用组件loade d扎成这个div。

  3. src/index.js: This is the javascript file corresponding to index.html. This file has the following line of code which is very significant. ReactDOM.render(<App />, document.getElementById(‘root’));

    src / index.js :这是与index.html对应的javascript文件。 该文件包含以下非常重要的代码行。 ReactDOM.render(<App />,document.getElementById('root'));

  4. The above line of code is telling that App Component ( will cover App Component soon) has to be loaded into an html element with id root. This is nothing but the div element present in index.html.

    上面的代码行告诉我们,必须将App Component(即将覆盖App Component)加载到id为root的html元素中。 这只是index.html中存在的div元素

  5. src/index.css: The CSS file corresponding to index.js.

    src / index.css :与index.js对应CSS文件。

  6. src/App.js : This is the file for App Component. App Component is the main component in React which acts as a container for all other components.

    src / App.js :这是应用组件的文件。 App Component是React中的主要组件,它充当所有其他组件的容器。

  7. src/App.css : This is the CSS file corresponding to App Component

    src / App.css :这是与应用程序组件相对应CSS文件

  8. build: This is the folder where the built files are stored. React Apps can be developed using either JSX, or normal JavaScript itself, but using JSX definitely makes things easier to code for the developer :). But browsers do not understand JSX. So JSX needs to be converted into javascript before deploying. These converted files are stored in the build folder after bundling and minification. In order to see the build folder Run the following command

    build:这是存储构建文件的文件夹。 可以使用JSX或常规JavaScript本身来开发React Apps,但是使用JSX无疑使开发人员更容易编写代码:)。 但是浏览器不了解JSX。 因此,在部署之前,需要将JSX转换为javascript。 捆绑和缩小后,这些转换后的文件将存储在build文件夹中。 为了查看构建文件夹,请运行以下命令

npm run build

创建组件 (Creating Components)

A Component in React does a specific functionality. An Application is nothing but a collection of components. Each component can have multiple child components and the components can communicate with each other.

React中的组件具有特定功能。 应用程序不过是组件的集合。 每个组件可以具有多个子组件,并且这些组件可以相互通信。

Let's create a React Component now.

现在创建一个React组件。

Inside src folder create a file called as FirstComponent.js and copy the following code into FirstComponent.js.

src文件夹中,创建一个名为FirstComponent.js的文件,并将以下代码复制到FirstComponent.js中。

import React, {Component} from 'react';export default class FirstComponent extends Component {constructor(props) {super(props)}render() {const element = (<div>Text from Element</div>)return (<div className="comptext"><h3>First Component</h3>{this.props.displaytext}{element}</div>)}
}
  1. The Component name is FirstComponent which is denoted by the file name as well as in the statement export default class FirstComponent extends Component

    组件名称是FirstComponent ,它由文件名以及在语句export default class FirstComponent extends Component

  2. The props attribute in the constructor will contain all the parameters which are passed as input to this component.

    构造函数中的props属性将包含所有作为输入传递到此组件的参数。

  3. render(): The return value of this function is rendered ( displayed ) on the screen. Whenever the render() function is called the Screen is rerendered. This is generally done automatically by the application. The code which looks very similar to html in this function is nothing but JSX.

    render():此函数的返回值在屏幕上呈现(显示)。 每当调用render()函数时,都会重新渲染屏幕。 通常,这是由应用程序自动完成的。 在此函数中看起来与html非常相似的代码不过是JSX。

JSX (JSX)

JSX looks very similar to HTML but has the full power of javascript. Here I will Explain the JSX code and what it is trying to do.

JSX看起来非常类似于HTML,但是具有javascript的全部功能。 在这里,我将解释JSX代码及其作用。

render() {const element = (<div>Text from Element</div>)return (<div className="comptext"><h3>First Component</h3>{this.props.displaytext}{element}</div>)}

The first line const element = (<div>Text from Element</div>) Creates a div element and assigns it to a constant called element. This peculiar Syntax that you see is nothing but JSX.

第一行const element = (<div>Text from Element</div>)创建一个div元素,并将其分配给一个称为element的常量。 这种奇特的语法,你看到的是荷兰国际集团使者诺斯但JSX。

Inside the Return statement, you see the following code syntax.

在Return语句内,您将看到以下代码语法。

<div className="comptext"><h3>First Component</h3>{this.props.displaytext}{element}
</div>

Here className is used to point to a CSS class. <h3>First Component</h3> is just normal html Syntax. {this.props.displaytext} is used to access an attribute called as displaytext from props ( so displaytext is passed as an input whenever this component is called ). Here displaytext is just a custom name that I have Given. {element} is the constant which was created, which in turn contains the div element.

在这里, className用于指向CSS类。 <h3>First Component</h3>只是普通的html语法。 {this.props.displaytext}用于访问来自props的称为displaytext的属性(因此,只要调用此组件, {this.props.displaytext} displaytext作为输入传递)。 这里displaytext只是我给定的一个自定义名称。 {element}是创建的常量,而常量又包含div元素。

使用组件 (Using the Component)

FirstComponent has been created but is not being used anywhere yet. Let's add FirstComponent to App Component. Here is the modified code for App.js

FirstComponent已创建,但尚未在任何地方使用。 让我们将FirstComponent添加到App Component中。 这是App.js的修改后的代码

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import FirstComponent from './FirstComponent'
class App extends Component {render() {return (<div className="App"><header className="App-header"><img src={logo} className="App-logo" alt="logo" /><h1 className="App-title">Welcome to React</h1></header><p className="App-intro">To get started, edit <code>src/App.js</code> and save to reload.</p><FirstComponent displaytext="First Component Data"/></div>
);}
}
export default App;

FirstComponent needs to be imported into App Component First which is done in the line import FirstComponent from ‘./FirstComponent’

首先需要将FirstComponent导入到App Component First中,这是import FirstComponent from './FirstComponent'中的import FirstComponent from './FirstComponent'行中完成import FirstComponent from './FirstComponent'

Then FirstComponent is added to App Component using the line <FirstComponent displaytext=”First Component Data”/>

然后使用<FirstComponent displaytext=”First Component Data”/> 行将FirstComponent添加到App Component。

Here displaytext is passed as an attribute to the FirstComponent.

这里displaytext作为属性传递给FirstComponent。

Now you can run the Application using the command npm start

现在,您可以使用命令npm start运行该应用npm start

You should see the following result in the browser.

您应该在浏览器中看到以下结果。

恭喜

reactjs快速如梦_帮助您理解和创建ReactJS应用的快速指南相关推荐

  1. bootstrap快速入门_在5分钟内学习Bootstrap 4-快速入门指南

    bootstrap快速入门 了解世界上最受欢迎的前端组件库的最新版本. (Get to know the newest version of the worlds most popular front ...

  2. python中流程图的基本元素_面试干货:成为Python程序员的终极指南!(内附回答)...

    科技行业正以前所未有的速度增长.我们看到市场上发布了新的软件产品. 无论您是初学者还是经验丰富的Python开发人员,机会都是非常多的,如何把握?. 唯一的要求:必须在Python编程面试中使用您的技 ...

  3. 交通流分析2:《基于公共交通大数据的上海市居民出行时空特征研究_王宇》和《面向交通拥堵预测大数据的神经网络群组快速学习_沈晴》阅读总结

    上一篇的地址:https://blog.csdn.net/qq_43012160/article/details/103313749 基于公共交通大数据的上海市居民出行时空特征研究_王宇 这篇论文内容 ...

  4. 快速傅里叶变换_计算物理基础:第八章-快速傅里叶变换(FFT)

    参考北京师范大学的<计算物理基础> 第八章-快速傅里叶变换 计算物理基础_中国大学MOOC(慕课)​www.icourse163.org 1.快速傅里叶变换 1.1 离散傅里叶变换及其变换 ...

  5. ERP流程入门_从会计分录理解企业基本流程[转]

    ERP流程入门_从会计分录理解企业基本流程 本贴写给尚未在企业工作过的朋友!了解企业的基本流程的一个方法是看它的会计分录,我们现在来看一个完整的企业基本流程,它的会计分录是如何做的,其中有些帐户名称可 ...

  6. 施工工期计算器在线_办公室装修工期要多久时间,办公室快速装修怎么做?

    办公室装修周期多久,是老板比较担心和顾虑的问题.现代商业办公空间的高效率.高租金压力.营业时间限额卡定等因素,一般项目装修,会要求在约定的进度内,完成所有的施工工程,提前交付工程验收.让老板能快速进入 ...

  7. Cell Metabolism:碳水化合物限制饮食对人类肝脂肪变性的快速代谢益处的综合理解

    本文转自"微生太",已获授权,有修改. 导读 碳水化合物限制饮食是干预非酒精性脂肪肝疾病(NAFLD)的广泛推荐措施,但对这种饮食的多种益处的系统性观点还很缺乏.本研究采用等热量低 ...

  8. golang快速入门[8.3]-深入理解IEEE754浮点数

    前文 golang快速入门[1]-go语言导论 golang快速入门[2.1]-go语言开发环境配置-windows golang快速入门[2.2]-go语言开发环境配置-macOS golang快速 ...

  9. 互联网流量是什么意思_谈谈我理解的流量

    互联网流量是什么意思_谈谈我理解的流量 我们先从标题说起,谈谈我理解的流量.流量前并没有加互联网,也没有加线上.线下,也没有加付费.免费,说明这个是包含所有流量.当然这个不包含手机流量那一类!为什么今 ...

最新文章

  1. 我国机器视觉企业体量偏小,上游零部件占利润大头
  2. 使用Laya引擎开发微信小游戏(下)
  3. Spring 事务底层原理,你会了吗?
  4. STM32开发 -- 设置MCU运行频率
  5. |Vijos|贪心|P1414 Dejected Birthday-盗窃
  6. The authenticity of host '0.0.0.0 (0.0.0.0)' can't be established.
  7. java jpa 异步编程_异步处理时的JPA
  8. MyBatis学习 之 三、动态SQL语句
  9. 尤克里里怎么样_尤克里里入门简单教程
  10. hadoop源码_HBASE源码导入IDEA并开启DEBUG调试
  11. Ubuntu18.04 命令行安装PyCharm
  12. Linux开机自动启动ORACLE设置
  13. java+jxls利用excel模版进行导出
  14. mysql 明文密码_后台能看到明文密码的处理
  15. 开源硬件USB抓包及协议分析工具分享
  16. win7 IE11下,无法通过Windows更新为其他微软产品获取更新
  17. 小红书的浏览量很低是为什么?有什么提高的方法吗?
  18. 实习期间工作、学习、成长、收获总结
  19. QCOM 8976 porting SPI device
  20. 28 关于 Finalizer

热门文章

  1. Python内置数据类型之Tuple
  2. 自学Android!Android高级工程师面试题-字节跳动,附答案
  3. 国内互联网公司算法机器学习岗(阿里星)面试总结
  4. Android View系列(二):事件分发机制源码解析
  5. 分库分表的几种常见形式以及可能遇到的难题
  6. 制作alipay-sdk-java包到本地仓库
  7. 30.Android之百度地图简单学习
  8. win 7中修改Hosts方法
  9. jenkins的JAVA简单顺序配置git仓库
  10. Weblogic 节点启动