简体   繁体   English

如何根据从帮助程序脚本导入的变量更改重新渲染组件?

[英]How to rerender component on variable change which is imported from a helper script?

I have something like the code below and I would like to check with you if there is a better way to do this.我有类似下面的代码,如果有更好的方法,我想与您联系。 Basically I have a helper script which gets data from a third party API. I would like the components that import this helper script to rerender every time the API is called.基本上我有一个帮助脚本,它从第三方 API 获取数据。我希望每次调用 API 时导入这个帮助脚本的组件重新呈现。 The API is called multiple times.多次调用 API。

I'm doing this right now:我现在正在这样做:

MyClass script:我的课程脚本:

import { rerenderComponentList } from '../../../helpers/myscript';

export default class MyClass extends LitElement {
  static get properties() {
    return {
      item: {},
    };
  }

  constructor() {
    super();
    this.item = [];

    rerenderComponentList(this);
  }

  render() {
    const icon = Iconify.renderSVG(this.item.icon, {});

    return html` <div>Hello</div> `;
  }

  static get styles() {
    return styles;
  }
}

The rerenderComponentList imported function is used to send the current element object to the helper script.导入的rerenderComponentList function 用于将当前元素object发送到helper脚本。

Helper script:帮助脚本:

const list = [];

export function rerenderComponentList(object) {
  list.push(object);
}

function rerenderComponent() {
  list.map((item) => item.requestUpdate());
}

function callbackIcons(loaded) {
  if (loaded.length) {
    rerenderComponent();
  }
}

//Function used to call API in app.js
export function loadIcons(icons) {
  API.myfunction(icons, callbackIcons);
}

The rerenderComponent function is used to call the requestUpdate on each element object. rerenderComponent function 用于在每个元素 object 上调用requestUpdate

Any better and nicer suggestions would be greatly appreciated!任何更好更好的建议将不胜感激!

This is where the new ReactiveController can help.这是新的ReactiveController可以提供帮助的地方。 This lets you hook in to the component lifecycle, so your API can tell the LitElement when it needs to render:这使您可以连接到组件生命周期,因此您的 API 可以告诉LitElement何时需要渲染:

import { ReactiveController, ReactiveControllerHost } from 'lit';

export class MyController 
    implements ReactiveController {

    list = [];

    constructor(private host: ReactiveControllerHost) {
        host.addController(this);
    }

    private callbackIcons(loaded) {
        this.list = loaded;
        this.host.requestUpdate();
    }
    
    loadIcons(icons) {
        API.myfunction(icons, items => this.callbackIcons(items));
    }
}

Then in your element:然后在你的元素中:

import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { MyController } from './my-controller.js';

@customElement('my-element')
class MyElement 
    extends LitElement {

    private myController = new MyController(this);

    render() {
        const icons = this.myController.list.map(i => Iconify.renderSVG(i)) ;
        return html`... ${icons} ...`;
    }
}

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

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