One of the great things about React is its flexible component system. Once you get a hang of it, you can break up your application into reusable components and include them all over your project.

React的一大优点是其灵活的组件系统。 一旦掌握了这些技巧,就可以将应用程序分解为可重用的组件,并将其包含在整个项目中。

The problem is that there are a few gotchas that make working with components difficult for those new to React.

问题是,有一些陷阱使React新手很难使用组件。

For example, say you have the following component, mainIntro.js:

例如,假设您具有以下组件mainIntro.js

const mainIntro = props => (<div id="quote-box"><h1> Hunter x Hunter Quotes </h1><div id="text">"When I say it doesn't hurt me, that means I can bear it."</div><div id="author">- Killua Zoldyck</div><button id="new-quote"> Next Quote </button><a href="#" id="tweet-quote" target="_blank"> Tweet this quote </a></div>
)export default mainIntro;

And want to import it into App.js:

并希望将其导入App.js

import mainIntro from './components'class App extends React.Component{render(){return(<mainIntro />);}
}const mainNode = document.getElementById("quoter");
ReactDOM.render(<App />,mainNode);

But mainIntro isn't loading for some reason. Let's take a closer look at what's happening.

但是mainIntro由于某种原因未加载。 让我们仔细看看发生了什么。

命名组件 (Naming your components)

For anyone familiar with Object Oriented Programming, it's common convention to name each class with an uppercase letter. For example, a class to describe a person would be called Person to indicate that it's a class.

对于熟悉面向对象编程的任何人来说,通常的约定是用大写字母命名每个类。 例如,一个描述一个人的类将被称为Person以表明它是一个类。

In React, which uses JSX rather than plain JavaScript, the first letter of a tag indicates what kind of element it is. Uppercase first characters are used to specify React components, so mainIntro should instead be called MainIntro:

在使用JSX而不是普通JavaScript的React中,标签的第一个字母表示元素的种类。 大写的第一个字符用于指定React组件,因此mainIntro应该改为MainIntro

const MainIntro = props => (<div id="quote-box"><h1> Hunter x Hunter Quotes </h1><div id="text">"When I say it doesn't hurt me, that means I can bear it."</div><div id="author">- Killua Zoldyck</div><button id="new-quote"> Next Quote </button><a href="#" id="tweet-quote" target="_blank"> Tweet this quote </a></div>
)export default MainIntro;

While the filename can still be mainIntro.js, it's a good idea to capitalize the first character, too. Later when you scan the contents of the directory, you'll quickly be able to pick out that MainIntro.js contains a component.

尽管文件名仍然可以是mainIntro.js ,但也最好将第一个字符大写。 稍后,当您扫描目录的内容时,您将很快能够发现MainIntro.js包含一个组件。

Now App.js should look like this:

现在App.js应该看起来像这样:

import MainIntro from './components/MainIntro.js';class App extends React.Component{render(){return(<MainIntro />);}
}const mainNode = document.getElementById("quoter");
ReactDOM.render(<App />,mainNode);

如何安装React? (How is React Installed?)

There are two main ways to use React. First, install and set it up locally, probably through create-react-app. Second, through a CDN.

有两种主要的方法来使用React。 首先,可能通过create-react-app在本地安装并设置它。 第二,通过CDN。

You might have noticed above that the code snippets don't actually include React in the project with import React from'react';. This will throw an error if you're working with React locally.

您可能在上面已经注意到,代码片段实际上并未在从“ import React from'react';的项目中包含import React from'react'; 。 如果您在本地使用React,这将引发错误。

However, if you're using a CDN to load React, it's available globally and you don't need to import it like above.

但是,如果您使用CDN加载React,那么它在全球范围内都是可用的,您不需要像上面那样导入它。

箭头功能 (Arrow Functions)

Before diving into React, it's important to have a solid understanding of JavaScript, particularly ES6 syntax.

在深入研究React之前,对JavaScript特别是ES6语法有扎实的了解非常重要。

Take another look at the MainIntro component:

再看一下MainIntro组件:

const MainIntro = props => (<div id="quote-box"><h1> Hunter x Hunter Quotes </h1><div id="text">"When I say it doesn't hurt me, that means I can bear it."</div><div id="author">- Killua Zoldyck</div><button id="new-quote"> Next Quote </button><a href="#" id="tweet-quote" target="_blank"> Tweet this quote </a></div>
)export default MainIntro;

If you look closely at the first line, you'll notice a syntax error:

如果仔细观察第一行,您会发现语法错误:

const MainIntro = props => (

You're writing a functional component, which are typically simple JavaScript functions that can accept props as an argument and return valid JSX. Of course, the syntax needs to be correct for it to return properly.

您正在编写一个功能组件,这些组件通常是简单JavaScript函数,可以接受props作为参数并返回有效的JSX。 当然,语法必须正确才能正确返回。

Arrow functions can be written in a lot of ways, but for this example, you'll need to add the curly braces ({}) and make sure to return JSX from the component itself:

Arrow函数可以通过多种方式编写,但是对于本示例,您需要添加花括号( {} ),并确保从组件本身返回JSX:

const MainIntro = props => {return (<div id="quote-box">//... rest of the code   </div>);
}

After implementing all the changes mentioned above, your component should now render properly.

完成上述所有更改后,您的组件现在应正确呈现。

Though the main distinction between functional and class components in React used to be that the former was "stateless" while the latter was "statefull", React Hooks have blurred the lines between them. Read more about both components in this overview and this deeper dive into functional components with React Hooks.

尽管React的功能和类组件之间的主要区别以前是前者是“无状态”,而后者是“有状态”,但是React Hooks混淆了它们之间的界线。 在此概述中了解有关这两个组件的更多信息,并通过React Hooks深入了解功能组件 。

翻译自: https://www.freecodecamp.org/news/react-simple-intro-component-not-rendering/

应对–简单的Intro组件无法渲染?相关推荐

  1. vue避免重新渲染_详解强制Vue组件重新渲染的方法

    在某些情况下,我们必须强制Vue重新渲染组件,如果没有,那可能,你做的业务还不够负责,反正我是经常需要重新渲染组件,哈哈. 虽然Vue不会自动更新这种情况是相对比较少,但是知道如何在出现这个问题时修复 ...

  2. json 微信小程序 筛选_GitHub - zhengyangkang/sl-filter: uni -app 一款使用简单的筛选组件,适配app、微信小程序、H5。...

    sl-filter 筛选 筛选组件,组件名:sl-filter dcloud插件市场地址 sl-filter 简介 一款使用简单的筛选组件,适配app.微信小程序.H5. 感谢分享 效果图 并列菜单 ...

  3. html页面渲染vue组件,Vue组件页面渲染的基本流程

    html: 组件页面渲染的基本流程 main.js: import Vue from "vue"; import Home from "./home.vue"; ...

  4. 深入了解React组件重新渲染的条件和生命周期

    React组件rerender的真正条件 当前组件的State中的属性改变时且当前组件的shouldcomponentupdate返回true,那么当前组件会rerender 组件的props中的任一 ...

  5. [react] 如何提高组件的渲染效率呢?

    [react] 如何提高组件的渲染效率呢? 类组件: 1.继承PureComponent 2.使用shouldComponentUpdate优化 函数组件: 1.memo模拟PureComponent ...

  6. shaderToy初学笔记(一)(​转载自最简单的ShaderToy入门 - 笑脸渲染_亨利王的博客-CSDN博客_shadertoy)

    void mainImage( out vec4 fragColor, in vec2 fragCoord ) {vec2 uv = fragCoord/iResolution.xy;uv-=.5;f ...

  7. 使用组件不渲染 Unknown custom element: <xxx> - did you register the component correctly? For recursiv

    使用组件偶尔会出现组件不渲染,报错的情况 可能出现的错误,是因为element-ui没有注册 解决方法: 在main.js/main.ts中下载并注册 有的小伙伴儿,可能会由于自己封装从而改变样式 那 ...

  8. React-Native Text组件重新渲染时会文字超出屏幕的问题

    React-Native Text组件重新渲染时会文字超出屏幕的问题 今天在开发中碰到一个很奇怪的问题就是初次渲染的时候Text组件里的文字会正常换行,一切看起来都很和谐,但是当我滚动scrollvi ...

  9. (Vue爬坑)子组件的渲染时间比父组件的渲染时间快导致数据的问题

    子组件的渲染时间比父组件的渲染时间快导致数据的问题 问题:父组件的在mounted钩子函数中拿接口的数据 ,然后渲染给子组件:因为子组件渲染的速度比父组件快,所以子组件的pros内的数据是空的 解决问 ...

最新文章

  1. microsoft word中在公式后插入可交叉引用的公式编号
  2. 开源的ResearchKit:苹果将如何颠覆未来医疗?
  3. 网页布局(固定与不固定原理)
  4. GD32如何替换STM32?
  5. scala(10)-----Scala 闭包
  6. 计算机管理创建新用户,win7系统添加新用户名的方法和win7系统计算机管理中没有本地用户和组的解决方法...
  7. nacos注册发现原理
  8. 2020/5/4/ 每日一咕
  9. 业余时间研究了下微信小程序版的街机游戏模拟器,8090后的童年回忆啊
  10. 云服务器无法访问解决办法
  11. python爬虫之多线程、多进程爬虫
  12. android监听耳机,Android监听耳机按键事件
  13. HTTP的请求常用方法
  14. 基于Quartus-FPGA制作蜂鸣器的相关教程
  15. ATFX:5年期LPR利率大降15基点,USDCNH涨破年内高点
  16. dedecms模板教程:织梦模板网站SEO优化教程
  17. PMP可以自学报考吗
  18. 使用正则限制输入框只能输入英文和数字
  19. 求职面试__无领导小组讨论__沉船逃生排序问题
  20. 全球与中国冷气喷涂市场深度研究分析报告

热门文章

  1. CSS——CSS创建样式表
  2. 【AI视野·今日CV 计算机视觉论文速览 第233期】Tue, 3 Aug 2021
  3. 【python】正则表达式匹配多个模式
  4. 匿名内部类 java 1614965228
  5. 安装虚拟环境virtualenv 适用于windows操作系统
  6. linux-文件的删除与创建
  7. 索引-python编程技术-第二版
  8. mysql报错Attempted to open a previously opened tablespace的解决办法
  9. git clone报错:Permission denied (publickey). fatal: Could not read from remote repository...
  10. 第三章 第一部分 不定积分例题