警告:未知的 DOM 属性类.你是说 className 吗? [英] Warning: Unknown DOM property class. Did you mean className?

查看:31
本文介绍了警告:未知的 DOM 属性类.你是说 className 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始探索 React,通过添加一个具有简单渲染功能的组件:

I've just started exploring React, by adding a component with a simple render function:

render() {
  return <div class="myApp"></div>
}

当我运行应用程序时,出现以下错误:

When I run the app, I get the following error:

Warning: Unknown DOM property class. Did you mean className?

我可以通过将 class 更改为 className 来解决这个问题.

I can solve this by changing class to className.

问题是;React 是否强制执行此约定?另外为什么我需要使用 className 而不是传统的 class?如果这是一个限制,那么是由于 JSX 语法还是其他原因?

The question is; does React enforce this convention? Also why do I need to use className instead of the conventional class? If this is a restriction then is it due to JSX syntax or somewhere else?

推荐答案

是的,这是 React 约定:

Yes, its a React convention:

由于 JSX 是 JavaScript,classfor 等标识符是不鼓励作为 XML 属性名称.相反,React DOM 组件期望 DOM 属性名称分别为 classNamehtmlFor.

Since JSX is JavaScript, identifiers such as class and for are discouraged as XML attribute names. Instead, React DOM components expect DOM property names like className and htmlFor, respectively.

JSX 深入.

这篇关于警告:未知的 DOM 属性类.你是说 className 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆