解构Codepen中的组件

时间:2018-10-31 08:47:36

标签: reactjs destructuring codepen

我只是在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"));

它不渲染,但是如果我将它编写为类,那么有人知道如何修复它吗?

1 个答案:

答案 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