Skip to content

Instantly share code, notes, and snippets.

@Tallyb
Created June 7, 2019 18:19
Show Gist options
  • Save Tallyb/1c7ecf19d3ab3e1ffde31c27594acd6f to your computer and use it in GitHub Desktop.
Save Tallyb/1c7ecf19d3ab3e1ffde31c27594acd6f to your computer and use it in GitHub Desktop.
Fetch component
import { Component, h, JSX, Prop, Watch } from '@stencil/core';
@Component({
tag: 'my-fetch',
styleUrl: 'fetch.css'
})
export class MyFetchComponent {
@Prop() language: string;
@Watch('language')
async onLanguageChange() {
if (this.language) {
return fetch('./assets/i18n/' + this.language + '.json')
.then(response => {
if (!response.ok) {
throw new Error('HTTP error ' + response.status);
}
return response.json();
})
.then(json => {
console.log('JSON', json);
});
}
}
componentWillLoad() {
if (this.language) {
this.onLanguageChange();
}
}
render(): JSX.Element {
return <slot></slot>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment