Files
react.dev/docs/ref-09-webcomponents.zh-CN.md
Daniel Lo Nigro d457201862 Upgrade to Jekyll 3
Full list of changes is at https://jekyllrb.com/docs/upgrading/2-to-3/. The tl;dr of it is:
 - Relative permalinks were removed, so all the files in the `docs` subdirectory need their permalink to be prefixed with `docs/`
 - `post` and `page` types were renamed to `posts` and `pages` respectively
 - `jekyll-paginate`, `pygments` and `redcarpet` are all now optional, so I needed to explicitly add it to the Gemfile. Jekyll now uses `rouge` rather than `pygments` for syntax highlighting, but rouge does not support highlighting individual lines (`hl_lines`) so we need to continue using Pygments.
 - Layout metadata (eg. `sectionid`) is now on a `layout` variable rather than `page`

Tested the following pages and confirmed that they all work:
 - "Docs" link (getting started page): http://127.0.0.1:4000/react/docs/getting-started.html
 - Downloads: http://127.0.0.1:4000/react/downloads.html
 - Tutorial: http://127.0.0.1:4000/react/docs/tutorial.html
 - A few pages under the "docs" subdirectory, to confirm they're working properly:
    - http://127.0.0.1:4000/react/docs/addons.html
 - http://127.0.0.1:4000/react/docs/why-react.html
 - http://127.0.0.1:4000/react/docs/create-fragment.html
 - A few tips:
    - http://127.0.0.1:4000/react/tips/false-in-jsx.html
 - http://127.0.0.1:4000/react/tips/style-props-value-px.html
 - Non-English versions of the page:
    - http://127.0.0.1:4000/react/docs/getting-started-it-IT.html
    - http://127.0.0.1:4000/react/docs/getting-started-ja-JP.html
2016-07-19 12:18:49 -07:00

2.1 KiB
Raw Blame History

id, title, permalink, prev, next
id title permalink prev next
webcomponents-zh-CN Web Components docs/webcomponents-zh-CN.html reconciliation-zh-CN.html glossary-zh-CN.html

因为React和Web组件是用来解决不同问题的, 试图比较两者将不可避免地导致似是而非的结论, 。Web组件为可复用组件提供了强大的封装, 而React则提供了一个使DOM与你的数据同步的声明式库。两者的目标是互补的工程师可以混合使用这些技术。作为一个开发者你可以在你的Web组件中使用React或者在React中使用Web组件或者两者一起使用。

在React中使用Web组件

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

注意:

两个组件系统的程序模型 (web 组件 与 react 组件) 区别在 web组件通常暴露一个命令式的API (比如 一个 video web 组件可能暴露 play()pause() 函数)。 因为web 组件是它们属性的声明式函数, 他们应该可以执行, 但是如果要访问命令式API的一个web组件, 你将需要在组件上附加一个引用 并与DOM节点直接交互。如果你在使用第三方web组件, 我们推荐你写一个React组件作为你的web组件的封装。

当前一个由web组件触发的事件可能不能准确地在React渲染树中传递。 你将需要手动附加event handlers来在你的react组件中处理这些事件。

在Web 组件中使用React

var proto = Object.create(HTMLElement.prototype, {
  attachedCallback: {
    value: function() {
      var mountPoint = document.createElement('span');
      this.createShadowRoot().appendChild(mountPoint);

      var name = this.getAttribute('name');
      var url = 'https://www.google.com/search?q=' + encodeURIComponent(name);
      ReactDOM.render(<a href={url}>{name}</a>, mountPoint);
    }
  }
});
document.registerElement('x-search', {prototype: proto});

完整样例

若要查看完整样例,请参照starter kit中的webcomponents例子。