javascript - What's the purpose of <div className="App"> in a React class render functio

I'm new to React, and so far I understand that className in React jsx can be a reference to an htm

I'm new to React, and so far I understand that className in React jsx can be a reference to an html class defined in an imported css file. Like className="warning" in the following example, which makes the text display red instead of the default black.

But I'm also seeing, in numerous tutorial examples, elements like <div className="App"> appearing at the top of the render() function. This typically doesn't reference anything in the imported css file, yet it can often be present even if there is no imported css file in the example at all.

Indeed I find that if I replace it with just <div>, the example renders exactly as before. So what do elements like <div className="App"> do, and what are they for?

App.js

import React, { Component } from 'react';
import './App.css';

class App extends Component {
    render() {
        return (
            <div className="App">
                <h1 className="warning">Wakeup World!</h1>
            </div>
        )
    }
}

export default App;

App.css

.warning {
    color: red
}

I'm new to React, and so far I understand that className in React jsx can be a reference to an html class defined in an imported css file. Like className="warning" in the following example, which makes the text display red instead of the default black.

But I'm also seeing, in numerous tutorial examples, elements like <div className="App"> appearing at the top of the render() function. This typically doesn't reference anything in the imported css file, yet it can often be present even if there is no imported css file in the example at all.

Indeed I find that if I replace it with just <div>, the example renders exactly as before. So what do elements like <div className="App"> do, and what are they for?

App.js

import React, { Component } from 'react';
import './App.css';

class App extends Component {
    render() {
        return (
            <div className="App">
                <h1 className="warning">Wakeup World!</h1>
            </div>
        )
    }
}

export default App;

App.css

.warning {
    color: red
}
Share Improve this question edited Mar 22, 2021 at 14:40 0stone0 44.5k5 gold badges52 silver badges80 bronze badges asked Mar 22, 2021 at 14:02 David WallaceDavid Wallace 3144 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

The purpose of <div className="App"> is nothing else but rendering a <div> using App as the class. If there's no styling linked to that className, you'd get the same result when changing it.

Since React has no official naming conventions, there isn't any official document that says that the first element must be a <div> containing the class App

I guess this has bee some sort of unspoken rule.

Even Facebooks create-react-app uses App as the first element. (Source)

However, React's tic-tac-toe tutorial doesn't use the App as first element, rather just a <div className="Game"> as it would make more sense.

I believe that this is just a convention, it does not pertain to React's internal rendering logic or anything like that.

To answer your question about why they are there, its for global styling changes that are a little more specific than html or body. Again, a convention that you can feel free to ignore.

Source: https://reactjs/docs/rendering-elements.html and related docs

Render div with class "App"

return (
         <div className="App">
           <h1 className="warning">Wakeup World!</h1>
         </div>
       )

Render h1 with class "warning"

return (
         <>
           <h1 className="warning">Wakeup World!</h1>
         </>
       )

or

return (
           <h1 className="warning">Wakeup World!</h1>
       )

This is the syntax of JSX, similar to html and renders what you ask. The examples use the default autogenerated template with `.., but this is not at all necessary

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745645454a4637943.html

相关推荐

  • 推荐一个轻量级的监控平台并且支持移动端

    简介XUGOU 是基于Cloudflare构建的轻量化监控平台,专精于系统资源监控与可视化状态页面服务。该平台提供英文简体中文双语支持,满足全球化部署需求。面向开发者及中小团队,项目致力于提供高可用性的监控解决方案。核心功能与实现平台功能

    47分钟前
    00
  • 大模型驱动金融数据应用的实战探索

    近年来,人工智能技术的飞速发展正在重塑全球各行各业的生态格局,金融行业作为数据密集型领域,更是首当其冲。大模型凭借其强大的自然语言处理、逻辑推理和生成能力,逐渐成为金融数据应用的核心驱动力。本文将从行业背景与趋势、核心场景重构、产品能力提升

    43分钟前
    00
  • maxwell遇到的一则问题

    结论和原因maxwell的元数据库里面没有存储全部的schema数据(就是少数据了),导致相关表的DDL校验失败。PS:我这里maxwell的作用只是采集库表修改情况的统计粗粒度指标,因为之前maxwell在运行报错的时候,直接修改了pos

    39分钟前
    00
  • Windows Server20192022 Evaluation评估版未激活导致关机问题

    摘要&#xff1a;在安装Windows Server 20192022后&#xff0c;会出现系统版本为 Evaluation 评估版情况&#xff0c;如提示Windows许可证已到期&#xff0c;就

    38分钟前
    00
  • 实现一个 MySQL 配置对比脚本需要考虑哪些细节?

    作者:李彬,爱可生 DBA 团队成员,负责项目日常问题处理及公司平台问题排查。爱好有亿点点多,吉他、旅行、打游戏爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。本文约 1500 字,预计阅读需要 3 分钟。引言想

    36分钟前
    00
  • 在VMware虚拟机中安装Windows 7全攻略(避坑指南)

    ⚠️写在前面 最近发现很多开发者在调试老旧系统时都需要用到Windows 7环境&#xff08;特别是银行、医疗等行业的遗留系统&#xff09;&#xff0c;但实体机安装既不现实也不安全。今天就手把手教你在虚拟机

    36分钟前
    00
  • windows新建open ai密钥

    api链接 openai的api需要付费才能使用但好像系统变量不知道为啥用不了打印出来&#xff0c;获取到的是None可以用了

    34分钟前
    00
  • 最后讲一遍:ChatGPT 快速生成国内外研究现状的方法

    在科研工作中,梳理国内外研究现状有助于明确研究方向,发现研究空白,为后续研究提供理论支持与创新思路。本文将详细介绍如何借助 ChatGPT 高效生成国内外研究现状,帮助您在有限时间内构建全面、专业的文献综述框架,提升学术写作效率与质量。St

    33分钟前
    00
  • Windows系统密钥检测工具PIDKey 2.1中文版

    Windows系统密钥检测工具PIDKey 2.1中文版 【下载地址】Windows系统密钥检测工具PIDKey2.1中文版 Windows系统密钥检测工具PIDKey 2.1中文版是一款功能强大的工具&#xff0c;专为管理Win

    31分钟前
    00
  • 1.54G 雨晨 26100.3775 Windows 11 IoT 企业版 LTSC 24H2 极速版

    精简AERO外主题并增加一套壁纸主题&#xff08;默认启用&#xff09;误杀导致功能界面空白、因WMIC被默认移除系统可能会多次重启。 拒止连接 www.5909 拒止连接 www.mnpc 拒止连接 quark 拒止

    30分钟前
    00
  • 雨晨 26200.5516 Windows 11 IoT 企业版 LTSC 2024 轻装版

    简述&#xff1a;以下为YCDISM (雨晨作品自2025年03月25日起通用介绍&#xff0c;若无重大更改不再额外敖述) 全程由最新YCDISM2025脱机装载26100.1742_zh-cn_windows_11_

    27分钟前
    00
  • 人工智能应用领域有哪些

    人工智能应用领域有哪些一、引言随着科技的飞速发展,人工智能(AI)已经逐渐渗透到我们生活的方方面面,成为推动社会进步的重要力量。从医疗健康到金融服务,从教育学习到智能制造,人工智能以其独特的技术优势,为各行各业带来了前所未有的变革。本文旨在

    23分钟前
    10
  • 在Windows上使用MetaMCP的完整指南

    在当今AI助手工具快速发展的时代&#xff0c;如何有效管理各种MCP&#xff08;Model Control Protocol&#xff09;服务成为了一个挑战。MetaMCP应运而生&#xff0c;它是

    19分钟前
    00
  • 设计模式:工厂方法模式(Factory Method)(2)

    当年做一个项目时,还不懂什么是设计模式,仅仅是按照经验完成了需求。回头看看,就是暗合桥接模式。但是,在整个需求实现过程中,甲方需要我在已经设计好的标准业务逻辑中添加非标的需求,因为,在他们眼里,从业务角度来看,是自然的拓展。如果当年我知道还

    19分钟前
    00
  • VoidZero 的野心,开发者的福音!

    前言昨天分享了尤雨溪公司 VoidZero 最新的产品 TSDown,我相信肯定有同学和我一样好奇,尤雨溪为什么要推出这么多工具,来增加大家的学习压力!今天我们从整体上分析下,这些产品的功能和目的!正文VoidZero 是尤雨溪于 2020

    17分钟前
    00
  • 电子产品设计与电源优化实用策略

    产品降成本是商业活动中的常见行为,可贯穿于产品设计、研发、生产、运输、销售及维护的各个环节。然而,降成本策略必须建立在对产品品质要求不降低的基础上,确保设计参数满足要求并通过相关测试。以下是具体优化与深度分析。研发工程师通常从设计入手,选择

    15分钟前
    00
  • OpenAI“Agent 圣经”翻车?LangChain 创始人怒怼“全是坑”!

    整理 | Tina当前,AI 领域呈现出一种近乎“追星式”的热情氛围,每当有新的东西发布,便迅速引发广泛关注与高度评价,仿佛技术变革即将一触即发。同时大家情绪也波动剧烈,从“危机论”到“爆发论”频繁切换。OpenAI 最近出的《A Pra

    14分钟前
    00
  • AI也要007?Letta、伯克利提出「睡眠时间计算」,推理效率翻倍还不加钱

    机器之心报道编辑:+0、陈陈AI 也要 007 工作制了!近日,AI 初创公司 Letta 和 UC 伯克利的研究人员提出了一种扩展人工智能能力的新方式 —— 睡眠时间计算(Sleep-time Compute),让模型在空闲时间「思考」,

    11分钟前
    10
  • Windows 10 LTSC 2019 中文版下载及安装教程(附安装包)

    &#xff08;cn_windows_10_enterprise_ltsc_2019_x64_dvd_9c09ff24&#xff09;涵盖常见疑问和注意事项&#xff1a; cn_windows_10_enterpr

    2分钟前
    10
  • 推荐2个.Net开源Html解析器,方便我们提取网页数据

    如果我们需要抓取网络上的数据,这时候我们就要写爬虫,这里面就涉及到网页的抓取、以及网页分析与数据提取。下面推荐2个.Net开源Html解析器,方便用于网络爬虫Html源码的解析、Html源码编辑等场景。一、AngleSharp这是一个基于.

    1分钟前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信