insomnia/app/ui/components/rendered-text.js

44 lines
773 B
JavaScript

// @flow
import * as React from 'react';
type Props = {
component: string,
children: string,
render: Function,
props?: Object
};
type State = {
renderedText: string
};
class RenderedText extends React.PureComponent<Props, State> {
constructor (props: any) {
super(props);
this.state = {
renderedText: ''
};
}
async _render () {
const {render, children} = this.props;
const renderedText = await render(children);
this.setState({renderedText});
}
componentDidMount () {
this._render();
}
componentDidUpdate () {
this._render();
}
render () {
const {component, props} = this.props;
return React.createElement(component, props || {}, this.state.renderedText);
}
}
export default RenderedText;