这是我的 React.Component 的 render
方法:
class Layout extends React.Component {
constructor() {
super();
this.state = {name: "Brian"};
}
changeName(name) {
this.setState({name});
}
render() {
return (
<h1 class="row">hello</h1>
);
}
}
ReactDOM.render(
<Layout/>,
document.getElementById('app')
);
这是渲染的 HTML:
<div id="app" class="container">
<h1 data-reactroot="">hello</h1>
</div>
我不明白,为什么h1
的class
属性消失了?
最佳答案
React 元素没有 class
属性。您必须使用 className
属性。
React 文档中提供了这样做的原因:
https://facebook.github.io/react/docs/jsx-in-depth.html#html-tags-vs.-react-components
关于javascript - 为什么 React 渲染的 "h1"元素中的 class 属性消失了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39234764/