我只是在Codepen中测试此代码
class App extends React.Component {
render() {
const greeting = 'Welcome to React';
return (
<div>
<Greeting greeting={greeting} />
</div>
);
}
}
const Greeting = ({ greeting }) => <h1>{greeting}</h1>;
React.render(<App />, document.getElementById("root"));
它不渲染,但是如果我将它编写为类,那么有人知道如何修复它吗?
答案 0 :(得分:1)
文件中是否导入了文件?
import React from "react";
import ReactDOM from "react-dom";
class App extends React.Component {
render() {
const greeting = 'Welcome to React';
return (
<div>
<Greeting greeting={greeting} />
</div>
);
}
}
const Greeting = ({ greeting }) => <h1>{greeting}</h1>;
ReactDOM.render(<App />, document.getElementById("root"));
看起来您已经在最后一行输入了。应该是ReactDom
这里是指向codeandbox的链接:https://codesandbox.io/s/5yl153nyxk