FastAPI+React全栈开发17 让我们创建一个React应用

Chapter04 Setting Up a React Workflow

17 Let’s Create a React App

FastAPI+React全栈开发17 让我们创建一个React应用

As I mentioned earlier, create-react-app takes away much of the heavy work when starting a project, and we will be using it throughout this book. However, bear in mind that there are other ways to set up React, for instance, you can include it through a CDN just like plain old jQuery if you want to go old-school!

正如我之前提到的,create-react-app在开始一个项目时省去了很多繁重的工作,我们将在本书中使用它。但是,请记住,还有其他方法可以设置React,例如,如果您想要老式的jQuery,您可以通过CDN将其包含在内!

Let’s create a simple app that we will be building upon in this introduction. Grab a folder, mine i called chapter4, cd into it, and from your Terminal of choice type in the following.

让我们创建一个简单的应用程序,我们将在此基础上进行构建。找到一个文件夹,我的名为chapter4, cd进去,然后从你选择的终端输入下面的内容。

npx create-react-app cars

Now atch the magic happen before your eyes! Npx is a tool that is included with the latest versions of npm, ant it allows us to run executable scripts without the need to install them on your machine. Please allow the process to finish. This can take a while and the output might be cryptic, but eventually, we will have a properly initiated React project ready to be developed.

现在看魔术发生在你眼前!Npx是最新版本的npm中包含的一个工具,它允许我们运行可执行脚本,而无需在您的机器上安装它们。请允许该过程完成。这可能需要一段时间,输出可能是模糊的,但最终,我们将有一个适当启动的React项目准备开发。

The terminal will inform you that you can run several commands, but at this point, we will follow the suggestion ant change the directory into the newly created cars directory (because that is what we called our project when we ran create-react-app) and run the following command.

终端将提示您可以运行几个命令,但此时,我们将按照建议将目录更改为新创建的cars目录(因为在运行create-react-app时,我们将项目命名为cars目录)并运行以下命令。

npm start

You will be greeted by a gray screen with a slowly rotating blue React logo. Success! However, behind this dead-simple page, there is tons of code, and we can examine this generated code by looking inside the cars folder that the good create-react-app robot (I always imagined it as a robot) built for us.

您将看到一个灰色的屏幕,上面有一个缓慢旋转的蓝色React徽标。成功!然而,在这个极其简单的页面背后,有大量的代码,我们可以通过查看为我们构建的优秀的create-react-app机器人(我总是把它想象成一个机器人)的cars文件夹来检查这些生成的代码。

The is a node_modules directory that, like in all Node.js projects, contains all the project dependencies, and there are lots of them! You will not need to touch this folder except in extreme debugging operations, so let’s move on to the next one. In the Public folder, there are a couple of generic files that we will soon remove, such as the PNG logs and the favicon.ico file, but there is also an extremely important HTML file - the index.html. This bar-bones file contains a div, with the id of the root and this div is the place where React will put our whole application.

它是一个node_modules目录,像所有Node.js项目一样,包含了所有的项目依赖项,而且有很多!除了极端的调试操作外,您不需要碰这个文件夹,所以让我们继续下一个。在Public文件夹中,有几个通用文件,我们很快就会删除,比如PNG日志和favicon.ico文件,但还有一个极其重要的HTML文件——index.html。这个条形文件包含一个div,它的id是根,这个div是React放置整个应用程序的地方。

Moving on to the src directory, this is where we will be doning all of our work. The App.js file that represents our entire application, all the components, menus, headers and footers, lists, and controls, will be hosted on this file, which, in turn, will be rendered in our single div with the id of the root in the HTML file. This monstrous complexity is necessary for the flexibility and capabilities that React will be able to provide us while developing, in just a few more steps.

转到src目录,我们将在这里完成所有的工作。代表我们整个应用程序的App.js文件,包括所有组件、菜单、页眉和页脚、列表和控件,都将托管在这个文件上,而这个文件又将呈现在我们的一个div中,它的id是HTML文件中的根目录。这种巨大的复杂性对于React在开发过程中能够提供给我们的灵活性和功能是必要的,只需要几个步骤。

Since I will not delve much into the styling of our React apps in this book, I want to get it out of the way as quickly as possible and in the easiest way possible in my opinion. React enables us to style applications in a myriad of ways, you can use classic CSS style sheets or SASS, you can opt for JavaScript-style objects, and there are very modern and weird but efficient solutions such as Styled Components. Additionally, all of the major virual frameworks have a React version - Material UI, Bootstrap, and Semantic UI - you name it. I will be using Tailwind CSS, which has an atypical approach that I like, and I feel that it doesn’t get in they way too much. I found Tailwind CSS excellent for defining basic, simple styles that make the page look simple and clean, while it is perfectly good for achieving pixel perfect designs from Figma or Adobe XD files if needed.

因为我不会在本书中深入探讨React应用程序的样式,所以我想以我认为最简单的方式尽快解决这个问题。React使我们能够以无数种方式为应用程序设计样式,您可以使用经典的CSS样式表或SASS,您可以选择javascript样式的对象,并且有非常现代和奇怪但有效的解决方案,例如Styled Components。此外,所有主要的虚拟框架都有一个React版本——Material UI、Bootstrap和Semantic UI——你能想到的都有。我将使用Tailwind CSS,它有一种我喜欢的非典型方法,我觉得它不会在他们的方式太多。我发现Tailwind CSS非常适合定义基本的、简单的样式,使页面看起来简单而干净,而如果需要的话,它非常适合从Figma或Adobe XD文件中实现像素完美的设计。

Tailwind CSS and Installation

Essentially, Tainwind CSS is a utility-first framework that translates CSS into a bunch of classes that can be used directly in the markup and enable us to achieve complex designs. Just by adding classes to our HTML elements, we will be able to create completely styled documents. Check it out on their excellent website at https://tailwindcss.com/ and get acquainted with it, as we will be using it for all our React-styling needs.

从本质上讲,Tailwind CSS是一个实用优先的框架,它将CSS转换成一堆可以直接在标记中使用的类,使我们能够实现复杂的设计。只要在HTML元素中添加类,就可以创建完全有样式的文档。在他们优秀的网站https://tailwindcss.com/上查看并熟悉它,因为我们将使用它来满足我们所有的react样式需求。

To install the Tailwind CSS framework in our cars project, we will follow the procedure from https://tailwindcss.com/docs/guides/create-react-app. Follow these steps.

要在我们的汽车项目中安装Tailwind CSS框架,我们将遵循https://tailwindcss.com/docs/guides/create-react-app上的步骤。遵循以下步骤。

First, we need to install the packages using npm. Stop your server with Ctrl+C and run the following.

首先,我们需要使用npm安装这些包。使用Ctrl+C停止服务器,然后运行以下命令。

npm install -D tailwindcss
npx tailwindcss init

Now, we need to tell Tailwind where to look for files. Open the tailwind.config.js file and make sure it contains the following.

现在,我们要告诉顺风公司去哪里找文件。打开tailwind.config.js文件,确保它包含以下内容。

/** @type {import('tailwindcss').Config} */
module.exports = {content: ["./src/**/*.{js,jsx,ts,tsx}",],theme: {extend: {},},plugins: [],
}

Finally, edit the index.css file in your src folder, delete everything - the styles that powered our rotating log page - and just leave the following.

最后,编辑src文件夹中的index.css文件,删除所有的内容——支持我们旋转日志页面的样式——只留下以下内容。

@tailwind base;
@tailwind components;
@tailwind utilities;

Edit the App.js file, clear everything and just leave the following.

编辑App.js文件,清除所有内容,只留下以下内容。

function App() {return (<div className="App"><h1 className="bg-slate-500 text-white text-center">This is a Tailwind styled site!</h1></div>);
}export default App;

Don’t worry about the weird className stuff, this is JSX, React’s language for creating HTML, but take a look at the classes (they are classes, despite the naming). The first one tells Tailwind to apply a background of slate-500 (it’s a color) and the text-white and text-center classes are pretty self-explanatory. You will notice that Visual Stutio Code does some nice autocompletion stuff as soon as you type in the first quote. Phew! We have a basic React + Tailwind setup. Now let’s move on and see whether it was worth it. If you want to practice Tailwind CSS a bit, try creating a full-height page with some creepy dashed borders! Next, we will tackle the most fundamental parts of React: JSX. This is the language that React uses to create the HTML and the components, the building blocks that will eventually compose our application.

不要担心奇怪的className之类的东西,这是JSX, React用来创建HTML的语言,但是看看这些类(它们就是类,尽管命名)。第一个代码告诉Tailwind应用slate-500(这是一种颜色)作为背景,text-white和text-center类是不言自明的。你会注意到Visual studio Code在你输入第一个引号时做了一些很好的自动补全功能。唷!我们有一个基本的React + Tailwind设置。现在让我们继续,看看这是否值得。如果你想练习一下顺风CSS,试着用一些令人毛骨悚然的虚线边框创建一个全高的页面!接下来,我们将处理React: JSX的最基本部分。这是React用来创建HTML和组件的语言,这些组件是最终组成应用程序的构建块。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/782568.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python中对象的创建于使用

创建对象 在Python中&#xff0c;创建对象是通过实例化类来完成的。一个类是一个包含属性和方法的蓝图&#xff0c;它定义了一种对象的行为和特征。我们可以使用class关键字来定义一个类&#xff0c;然后使用类的名称和括号来实例化对象。 以下是一个创建对象的例子&#xff…

使用Docker搭建Sonarr

Sonarr是一款用于自动化管理电视节目的开源应用程序。它能够根据用户设定的规则自动搜索、下载并整理电视剧集&#xff0c;支持多用户、多队列管理&#xff0c;并且可以与下载客户端如SABnzbd、NZBGet等集成&#xff0c;以及与媒体服务器如Plex、Emby等配合使用&#xff0c;为用…

【3】3道链表力扣题:删除链表中的节点、反转链表、判断一个链表是否有环

3道链表力扣题 一、删除链表中的节点&#x1f30f; 题目链接&#x1f4d5; 示例&#x1f340; 分析&#x1f4bb; 代码 二、反转链表&#x1f30f; 题目链接&#x1f4d5; 示例&#x1f340; 分析① 递归② 迭代 三、判断一个链表是否有环&#x1f30f; 题目链接&#x1f4d5; …

ES6 模块化操作

ES6模块化主要有两个操作&#xff1a;import 和 export 如果在html文件的script中引用模块的话&#xff0c;要设置<script type"module"> 一种导入导出方法&#xff1a; a.js//分别暴露 export let num 1 export function compute(a, b){return a b }//统…

C++中使用虚函数实现多态

虚函数是C中用于实现多态&#xff08;Polymorphism&#xff09;的重要特性。下面是关于虚函数的讲解和代码示例&#xff1a;### 虚函数的定义&#xff1a; 虚函数是在基类中声明为 virtual 的成员函数。 在派生类中重写&#xff08;override&#xff09;这个虚函数&#xff0c;…

数据结构与算法 循环双链表基本运算与对称算法

一、实验内容 1、实现循环双链表的各种基本运算的算法 &#xff08;1&#xff09;初始化循环双链表h &#xff08;2&#xff09;依次采用尾插法插入a,b,c,d,e元素 &#xff08;3&#xff09;输出循环双链表h&#xff1b; &#xff08;4&#xff09;输出循环双链表h长度&am…

物联网学习1、什么是 MQTT?

MQTT&#xff08;Message Queuing Telemetry Transport&#xff09;是一种轻量级、基于发布-订阅模式的消息传输协议&#xff0c;适用于资源受限的设备和低带宽、高延迟或不稳定的网络环境。它在物联网应用中广受欢迎&#xff0c;能够实现传感器、执行器和其它设备之间的高效通…

HSP_04章_扩展: 进制、位运算

文章目录 10. 扩展: 进制11. 位运算11.1 二进制在运算中的说明11.2 原码 反码 补码11.3位运算符11.3.1 ~按位取反11.3.2 &按位与11.3.3 ^按位异或11.3.4 |按位或11.3.5 << 左移11.3.6 >> 右移 10. 扩展: 进制 进制介绍 进制的转换 2.1 其他进制转十进制 二进…

面试八股——redis——集群

0. redis集群的方案 1.主从复制&#xff08;高并发读&#xff09; 一个主节点负责写操作&#xff08;增删改&#xff09;&#xff0c;多个从节点负责查操作。 主从复制是让主节点修改数据之后&#xff0c;将对应数据同步到从节点中。 2.哨兵模式&#xff08;实现高可用&#x…

Redis命令请求的执行过程(一)

命令请求的执行过程 概述 一个命令请求从发送到获得回复的过程中&#xff0c;客户端和服务器需要完成一系列操作。 例子 举个例子。如果我们使用客户端执行以下命令: 127.0.0.1:6379> SET KEY VALUE OK那么客户端发送SET KEY VALUE命令到获得回复OK期间&#xff0c;客户…

图片标注编辑平台搭建系列教程(6)——fabric渲染原理

原理 fabric的渲染步骤大致如下&#xff1a; 渲染前都设置背景图然后调用ctx.save()&#xff0c;存储画布的绘制状态参数然后调用每个object自身的渲染方法最后调用ctx.restore()&#xff0c;恢复画布的保存状态后处理&#xff0c;例如控制框的渲染等 值得注意的是&#xff0…

精品丨PowerBI负载测试和容量规划

当选择Power BI作为业务报表平台时&#xff0c;如何判断许可证的选择是否符合业务需求&#xff0c;价格占了主导因素。 Power BI的定价是基于SKU和服务器内核决定的&#xff0c;但是很多IT的负责人都不确定自己公司业务具体需要多少。 不幸的是&#xff0c;Power BI的容量和预期…

54 npm run serve 和 npm run build 输出的关联和差异

前言 通常来说 我们开发的时候一般会用到的命令是 “npm run serve”, “npm run build” 前者会编译当前项目, 然后将编译之后的结果以 node 的形式启动一个服务, 暴露相关业务资源, 因此 我们可以通过 该服务访问到当前项目 后者是编译当前项目, 然后做一下最小化代码的优…

第六讲 B+树索引

1 B树大家庭 有一种称为 B 树的特定数据结构&#xff0c;人们还使用该术语来泛指一类平衡树数据结构&#xff1a; B-Tree (1971)BTree (1973)B*Tree (1977?)B link-Tree (1981)Bε-Tree (2003)Bw-Tree (2013) 2 B树 BTree 是一种自平衡【self-balance】、有序【ordered】的…

文生图大模型Stable Diffusion的前世今生!

1、引言 跨模态大模型是指能够在不同感官模态(如视觉、语言、音频等)之间进行信息转换的大规模语言模型。当前图文跨模态大模型主要有&#xff1a; 文生图大模型&#xff1a;如 Stable Diffusion系列、DALL-E系列、Imagen等 图文匹配大模型&#xff1a;如CLIP、Chinese CLIP、…

LeetCode Python - 84. 柱状图中最大的矩形

目录 题目描述解法方法一方法二 运行结果方法一方法二 题目描述 给定 n 个非负整数&#xff0c;用来表示柱状图中各个柱子的高度。每个柱子彼此相邻&#xff0c;且宽度为 1 。 求在该柱状图中&#xff0c;能够勾勒出来的矩形的最大面积。 示例 1: 输入&#xff1a;heights …

C++高频面试知识总结 part1

面向对象 1.什么是类&#xff1f;2.面向对象程序设计思想&#xff1f;3.多态的实现&#xff1f;4.动态多态的作用&#xff1f;5.动态绑定的实现&#xff1f;6.纯虚函数的作用以及实现&#xff1f;7.虚函数表如何维护&#xff1f;推荐阅读 8.C struct和类的区别&#xff1f;9.C中…

pytorch常用的模块函数汇总(1)

目录 torch&#xff1a;核心库&#xff0c;包含张量操作、数学函数等基本功能 torch.nn&#xff1a;神经网络模块&#xff0c;包括各种层、损失函数和优化器等 torch.optim&#xff1a;优化算法模块&#xff0c;提供了各种优化器&#xff0c;如随机梯度下降 (SGD)、Adam、RMS…

手机投屏到windows11电脑

1 安装无线投影组件 2 电脑端打开允许其他设备投影的开关 3 手机找到投屏选项 4 手机搜索可用设备连接即可 这里的官方文档给的不太好,给了一些让人眼花撩乱的信息,以下是经过整合的有效信息

每日一题 --- 四数之和[力扣][Go]

四数之和 题目&#xff1a;18. 四数之和 给你一个由 n 个整数组成的数组 nums &#xff0c;和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] &#xff08;若两个四元组元素一一对应&#xff0c;则认为两个四元组…