zoukankan      html  css  js  c++  java
  • React文档(二十三)Web Components

    React和web components是为了解决不同问题而创立的。web components为可重用组件提供了健壮的封装,而React提供了声明式的库来保持DOM和数据同步。这两点是互相补充的。作为一个开发者,你可以自由地在你的web components里使用React,或者在React里使用web components,或者两者同时使用。

    很多人使用React而不使用web components,但是你也许想要试一试,特别是如果你在使用依靠web components开发的第三方UI组件。

    在React里使用web components

    class HelloMessage extends React.Component {
      render() {
        return <div>Hello <x-search>{this.props.name}</x-search>!</div>;
      }
    }

    注意:

    web components经常对实例暴露一个重要的接口,一个video web component也许会暴露play()和pause()函数。想要访问web component的接口,你将需要使用一个ref去与DOM节点直接交互。如果你使用第三方的web components,最好的解决方案就是写一个React组件来包裹web component。

    web components发出的事件也许不能正确地在React渲染树里传递。你将需要手动附加上事件处理器去处理这些事件在你的React组件里。

    有一个混淆就是web component使用class而不是className。

    function BrickFlipbox() {
      return (
        <brick-flipbox class="demo">
          <div>front</div>
          <div>back</div>
        </brick-flipbox>
      );
    }

    在web component里使用React

    const proto = Object.create(HTMLElement.prototype, {
      attachedCallback: {
        value: function() {
          const mountPoint = document.createElement('span');
          this.createShadowRoot().appendChild(mountPoint);
    
          const name = this.getAttribute('name');
          const url = 'https://www.google.com/search?q=' + encodeURIComponent(name);
          ReactDOM.render(<a href={url}>{name}</a>, mountPoint);
        }
      }
    });
    document.registerElement('x-search', {prototype: proto});

    你也可以看看这个链接完整的web component例子

  • 相关阅读:
    java生成验证码
    SpringBoot定时任务
    事务管理
    Windows 2008 Server R2双网卡负载均衡
    HP Proliant DL580 gen9 阵列卡P440AR 高速缓存 被禁用
    Kali Debian 修改时区
    First Py From Py
    C++头文件#include<bits/stdc++.h>
    排序算法
    运算符优先级
  • 原文地址:https://www.cnblogs.com/hahazexia/p/6491748.html
Copyright © 2011-2022 走看看