简体   繁体   English

将React库添加到网站时,如何将class组件转换为功能组件?

[英]How can I convert class component into functional component when adding React library to a Website?

I want to use react component in html file.我想在 html 文件中使用反应组件。 It is for micro frontend.它适用于微前端。 So I follow this react official doc for Add React to a Website .所以我按照这个 React 官方文档将 React 添加到网站

The sample code is as below:示例代码如下:

index.html index.html

<html>
  <body>
    
    <p>
      This is the first like.
      <div class="like_button_container" data-commentid="1"></div>
    </p>

    <p>
      This is the second like.
      <div class="like_button_container" data-commentid="2"></div>
    </p>

    <p>
      This is the third like.
      <div class="like_button_container" data-commentid="text"></div>
    </p>

    <script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin></script>

    <script src="like_button.js"></script>

  </body>
</html>

like_button.js like_button.js

"use strict";

const e = React.createElement;

class LikeButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = { liked: false };
  }

  render() {

    return e(
      "button",
      {
        onClick: () =>
          this.setState((prevState) => ({
            liked: !prevState.liked,
          })),
      },
      this.state.liked ? "Liked " + this.props.commentID : "Unliked"
    );
  }
}

document.querySelectorAll(".like_button_container").forEach((domContainer) => {
  const commentID = domContainer.dataset.commentid;
  ReactDOM.render(e(LikeButton, { commentID: commentID }), domContainer);
});

Above code is working fine but I want to convert like_button.js , which is class component into functional component.上面的代码工作正常,但我想将class组件 like_button.js 转换为功能组件。

Thanks in advance.提前致谢。

You need to use babel standalone script to transcompile the code, and you need to include the script for react and react-dom , use these will work.您需要使用babel standalone script来转换代码,并且需要包含reactreact-dom的脚本,使用它们就可以了。

<html>
<head>
    <script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
    <script crossorigin src="https://unpkg.com/@babel/standalone@7.15.7/babel.min.js"></script>
</head>
<body>
    <div id="react-container"></div>
    <script type="text/babel">
        const App = () => <div>Hello!</div>
        ReactDOM.render(
        <App />, document.getElementById('react-container'))
    </script>
</body>
</html>

Babel Standalone converts ECMAScript 2015+ into the compatible version of JavaScript for your browser, CDN usage is described in official documentation, check babel standalone section: Babel Standalone Babel Standalone 将 ECMAScript 2015+ 转换为适用于您的浏览器的 JavaScript 兼容版本,CDN 的使用在官方文档中有描述,查看 babel standalone 部分: Babel Standalone

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将此 Class 组件 function 转换为 React Native 中的功能组件 - How can I convert this Class component function into functional component in React Native 我如何编写此类组件来响应功能组件。? - How can i write this class component to react functional component.? 反应:如何访问 class 组件中的功能组件的道具 - React: How can I acces to props of a functional component in a class component 我怎样才能把这个 React class 组件变成一个功能组件? - How can i turn this React class component into a functional component? 如何将基于类的组件中的函数转换为 React 中的函数式组件? - How can I convert functions in class-based component to functional components in React? 如何使用 React Hooks 将此级联下拉 class 组件转换为功能组件? - How can I convert the this cascading dropdown class component to functional one using React Hooks? React 将类组件转换为功能组件不起作用 - React Convert class component to Functional component not working 在 React 中将功能组件转换为类组件 - Convert functional component to class component in React React - 将 class 组件转换为 session 的功能组件 - React - Convert class component to functional component for session 将 class 组件转换为 React 中的功能组件 - Convert class component to functional component in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM