简体   繁体   English

如何将回调函数从 React 传递给 litelement?

[英]How to pass callback function to litelement from React?

I'm trying to use lit-element component in my React project, and I'd like to pass in the callback function to lit-element component from React but with no luck.我正在尝试在我的 React 项目中使用 lit-element 组件,我想将回调函数传递给 React 的 lit-element 组件,但没有运气。

I've tried a couple of different ways, like change property type, and pass function in as a string, but none of them works.我尝试了几种不同的方法,例如更改属性类型,并将函数作为字符串传入,但它们都不起作用。

lit-element component code: lit-element 组件代码:

import { LitElement, html } from "lit-element";

class MyButton extends LitElement {
  static get properties() {
    return {
      clickHandler: {
        type: String
      },
      bar: {
        type: String
      }
    };
  }

  render() {
    const foo = this.clickHandler; //value is undefined
    const bar = this.bar; //value is "it's bar"
    return html`
      <button @click=${this.clickHandler}>click me</button>
    `;
  }
}

customElements.define("my-button", MyButton);

react side code:反应侧代码:

<my-button clickHandler={() => alert("clicked")} bar="it's bar" />

I put a break point in the render section of the component, and I can see the 'bar' value get passed in correctly, but the value of 'clickHandler' is undefined.我在组件的渲染部分放置了一个断点,我可以看到 'bar' 值被正确传入,但 'clickHandler' 的值未定义。

Does anyone have any idea on how to pass function from React to lit-element?有没有人知道如何将函数从 React 传递到 lit-element?

Thanks!谢谢!

This question probably isn't just valid for react.这个问题可能不仅仅适用于反应。 It's quite general, how do I pass a handler from parent to child component, either it's a lit-element or html element.这是非常通用的,我如何将处理程序从父组件传递到子组件,它是一个 lit-element 或 html 元素。

According to the property doc, https://lit-element.polymer-project.org/guide/properties根据财产文档, https://lit-element.polymer-project.org/guide/properties

<my-element 
  mystring="hello world"
  mynumber="5"
  mybool
  myobj='{"stuff":"hi"}'
  myarray='[1,2,3,4]'>
</my-element>

Doesn't seem that it supports (callback) function at the moment.目前似乎不支持(回调)功能。 So how does Element handle event from parent level ?那么 Element 如何处理来自父级的事件?

According to the event doc, https://lit-element.polymer-project.org/guide/events , you can dispatch any event to the dom tree, including your parent.根据事件文档https://lit-element.polymer-project.org/guide/events ,您可以将任何事件分派到 dom 树,包括您的父级。 Dom event system is much broader than React prop system. Dom事件系统比React道具系统广泛得多。

class MyElement extends LitElement {
    ...
    let event = new CustomEvent('my-event', {
      detail: {
        message: 'Something important happened'
      }
    });
    this.dispatchEvent(event);
}

and then in either lit or non-lit context, use the following to handle the event,然后在点亮或不点亮的上下文中,使用以下内容来处理事件,

const myElement = document.querySelector('my-element');
myElement.addEventListener('my-event', (e) => {console.log(e)});

This way you can allow children to fire implementation for parent, which is exactly the definition for callback.通过这种方式,您可以允许子级为父级触发实现,这正是回调的定义。

What I found that works is adding a ref in the react component to the lit element, then literally setting the property on it.我发现有效的是在反应组件中添加一个 ref 到 lit 元素,然后在其上设置属性。

So, for the following JSX:因此,对于以下 JSX:

<some-webcomponent ref={this.myRef}></some-webcomponent>

You can pass a property to 'some-webcomponent' in ie componentDidMount:您可以在 ie componentDidMount 中将属性传递给“some-webcomponent”:

componentDidMount () {
  const element = this.myRef.current;
  element.someCallback = () => // ...
}

It's not too pretty, but I wouldn't consider it a hack either.它不是太漂亮,但我也不会认为它是一个黑客。 Requires quite a lot of boilerplate though :/虽然需要相当多的样板:/

Here's a full React component for reference:这是一个完整的 React 组件供参考:

class MyComponent extends React.Component {   
  constructor(props) {     
    super(props);     
    this.myRef = React.createRef();   
  }   

  render() {     
    return <some-webcomponent ref={this.myRef}></some-webcomponent>;   
  } 

  componentDidMount() {
    const element = this.myRef.current;  
    element.someCallback = () => console.log(“call!”);
  }
}

Where the lit element is:点亮的元素是:

import { LitElement, html } from "lit-element";  

class SomeWebcomponent extends LitElement {
   static get properties() {    
     return {       
      someCallback: { type: Function }
     };
   }

   render() {     
     return html`       
       <button @click=${this.someCallback}>click me</button>     
     `;   
    } 
}  

customElements.define("some-webcomponent", SomeWebcomponent);

Have a look at the menu button click function for the no redux pwa-starter-kit at https://github.com/Polymer/pwa-starter-kit/blob/template-no-redux/src/components/my-app.js .https://github.com/Polymer/pwa-starter-kit/blob/template-no-redux/src/components/my-app查看 no redux pwa-starter-kit 的菜单按钮单击功能.js I believe that provides the example that may work for you.我相信这提供了可能对您有用的示例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM