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

44 lines
773 B
JavaScript
Raw Normal View History

2017-08-22 22:30:57 +00:00
// @flow
import * as React from 'react';
2017-08-22 22:30:57 +00:00
type Props = {
component: string,
children: string,
render: Function,
props?: Object
};
2017-08-22 22:30:57 +00:00
type State = {
renderedText: string
};
2017-08-22 22:30:57 +00:00
class RenderedText extends React.PureComponent<Props, State> {
2017-08-22 22:30:57 +00:00
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 () {
2017-08-22 23:54:31 +00:00
const {component, props} = this.props;
return React.createElement(component, props || {}, this.state.renderedText);
2017-08-22 22:30:57 +00:00
}
}
export default RenderedText;