react 组件样式类_React2020 P5类组件

news/2024/7/10 2:42:56 标签: react, vue, js, javascript, react native

react 组件样式类

In the two previous articles we looked at functional components.

在前两篇文章中,我们介绍了功能组件。

react-2020-p3-functional-components-28820c25d884" rel="noopener noreferrer">
react-2020-p4-functional-component-props-c870d18175fb" rel="noopener noreferrer">

Although the React team seems like they want to move away from class-based components, they’re still here. As a side note, I like and prefer class components. We’ve touched on class components in the Deeper Intro article.

尽管React团队似乎希望摆脱基于类的组件,但他们仍然在这里。 作为附带说明,我喜欢并喜欢类组件。 我们已经在“ 更深入的介绍”文章中介绍了类组件。

react-2020-p2-deeper-intro-df82d7beee40" rel="noopener noreferrer">

A class based component is a class that extends React.Component and contains a render() method that returns a JSX element. We can convert functional components into class components pretty easily.

基于类的组件是扩展React.Component的类,并包含一个返回JSX元素的render()方法。 我们可以很容易地将功能组件转换为类组件。

Let’s create a class component that lists a movie where Jim Carrey was awesome. We’ll start by creating a Sonic.js file inside of our src/components directory. If you haven’t seen Sonic yet, I highly recommend it (especially if you have kids).

让我们创建一个类组件,其中列出了Jim Carrey很棒的电影。 我们将从在src / components目录内创建一个Sonic.js文件开始。 如果您还没有看到Sonic ,我强烈推荐您(特别是如果您有孩子的话)。

Image for post

Just like with functional components, we need to import React.

就像功能组件一样,我们需要导入React。

import React from 'react';

The class will extend React.Component and will contain a render() method that returns an element. The render() method is a life-cycle method (we’ll look at life-cycle methods in later articles). The render() life-cycle method is a required method meaning that each time that you create a class component, that component will need to have the render() method.

该类将扩展React.Component并将包含一个返回元素的render()方法。 render()方法是生命周期方法(我们将在后面的文章中介绍生命周期方法)。 render()生命周期方法是必需的方法,这意味着每次创建类组件时,该组件都将需要具有render()方法。

Just like functional components, class components will need to be exported so that they can be rendered later on. The importing process is the same as with functional components. We’ll import the class component inside of the App component and render it.

与功能组件一样,类组件也需要导出,以便以后可以呈现。 导入过程与功能组件相同。 我们将把类组件导入App组件内部并进行渲染。

Make sure you have your development server running: npm start.

确保您的开发服务器正在运行: npm start

Image for post

Since we don’t know how to pass arguments yet to class components, to display an additional movie by Jim Carrey, you’ll have to create a new component. So, create the component Mask.js in src/components, import it into App and render it below <Sonic />.

由于我们尚不知道如何将参数传递给类组件,因此要显示Jim Carrey的其他影片,您必须创建一个新组件。 因此,在src / components中创建组件Mask.js ,将其导入App并在<Sonic />下呈现。

Image for post
Image for post

You might have noticed that we’re importing a class component into the App functional component. Yes, you can do that. You can also import functional components into class components. We’re going to be looking at passing arguments to components in the next article since we don’t want to keep creating individual components for each movie that Jim Carrey has appeared in.

您可能已经注意到,我们正在将类组件导入到App功能组件中。 是的,你可以这样做。 您也可以将功能组件导入类组件。 由于我们不想继续为Jim Carrey出演的每部电影创建单独的组件,因此我们将在下一篇文章中讨论将参数传递给组件。

react-youtube-tutorials" target="_blank" rel="noopener noreferrer">

翻译自: https://medium.com/dev-genius/react-2020-p5-class-components-2c36c0eb4c28

react 组件样式类


http://www.niftyadmin.cn/n/1468856.html

相关文章

如何使用php做关键字搜索,javascript、php关键字搜索函数的使用方法

javascript&#xff1a;a. 代码&#xff1a;/*desc&#xff1a;js搜索函数&#xff0c;可用于关键字匹配param key 关键字param str 要搜索的字符串return out 匹配关键字前后出现的位置*/function search(key,str){var min 0var max str.lengthvar index str.indexOf(key)v…

graphql 标量类型_使用graphql类型的graphql订阅

graphql 标量类型Being a developer it’s always cool to explore new libraries and technology.作为开发人员&#xff0c;探索新的库和技术总是很酷的。 Being fed up with REST API’s, I moved to GraphQL last year. In simple words, GraphQL is not a 100% solution f…

使用 fftw matlab mex,关于C#:使用minGW将FFTW链接到matlab

我的目标是混合使用库FFTW的C代码。#include #include #include"C:\\Users\\my_user_name\\Documents\\fftw-3.3.5-dll64\\fftw3.h"void mexFunction ( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){int i, j, bw, bw2_1, size, size2_1, nrow, nc…

gatsby_在gatsby中共享网站预览图像

gatsbyWhen sharing web content across phone, tablet, or laptop, image previews of a website provide a valuable first impression. If you’re developing websites using static site generators in the React ecosystem, such as Gatsby or Next.js, it’s not immedi…

小鱼易连电脑版_瞩目、腾讯会议、好视通、小鱼易连各有什么优势?怎么收费?...

视频会议并不是一个新鲜事物&#xff0c;存在已30多年&#xff0c;以传统厂商宝利通、思科、华为等大公司为代表&#xff0c;其时早就抢占了政府、金融、教育等领域以及各大公司。但在中小微公司&#xff0c;视频会议设备并不普及&#xff0c;要知道&#xff0c;动辄几十万的一…

matlab设置稀疏矩阵非零元素的个数,Matlab(12)——稀疏矩阵

Matlab(12)——稀疏矩阵文章目录Matlab(12)——稀疏矩阵一、矩阵的存储方式1.完全存储方式2.稀疏存储方式二、稀疏存储方式的产生1.矩阵的完全存储方式与稀疏存储方式的转换2.直接建立稀疏存储方式的矩阵①sparse函数②spconvert函数3.带状稀疏矩阵的稀疏存储方式①从带状稀疏矩…

_带你全面认识 Linux

作者 | arvin译者 | Enmanuel&#xff0c;责编 | 屠敏头图 | CSDN 下载自东方 IC出品 | CSDN(ID&#xff1a;CSDNnews)以下为译文&#xff1a;你知道哪个操作系统是世界上最受欢迎的操作系统吗&#xff1f;是Linux&#xff01;它一般运行于服务器和超级计算机上&#xff0c;我们…

如何在角度上创建无限滚动

If you’ve ever had to deal with large lists in your web app, then you’re probably familiar with infinite scrolling lists. Infinite scrolling is a common UX solution to the problem of presenting a large list to your users. Think about the Facebook feed or…