mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-22 16:46:34 +03:00
fix: make ReactStandalone react on props changes
This commit is contained in:
parent
18ec3ac17b
commit
0cb0af2cae
|
@ -3,6 +3,7 @@ import { Component } from 'react';
|
||||||
import { AppStore } from '../services/';
|
import { AppStore } from '../services/';
|
||||||
import { RedocRawOptions } from '../services/RedocNormalizedOptions';
|
import { RedocRawOptions } from '../services/RedocNormalizedOptions';
|
||||||
import { loadAndBundleSpec } from '../utils';
|
import { loadAndBundleSpec } from '../utils';
|
||||||
|
import { OpenAPISpec } from '../types';
|
||||||
|
|
||||||
interface StoreProviderProps {
|
interface StoreProviderProps {
|
||||||
specUrl?: string;
|
specUrl?: string;
|
||||||
|
@ -23,6 +24,8 @@ interface StoreProviderState {
|
||||||
export class StoreProvider extends Component<StoreProviderProps, StoreProviderState> {
|
export class StoreProvider extends Component<StoreProviderProps, StoreProviderState> {
|
||||||
store: AppStore;
|
store: AppStore;
|
||||||
|
|
||||||
|
private _resolvedSpec: OpenAPISpec;
|
||||||
|
|
||||||
constructor(props: StoreProviderProps) {
|
constructor(props: StoreProviderProps) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
|
@ -43,10 +46,21 @@ export class StoreProvider extends Component<StoreProviderProps, StoreProviderSt
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const resolvedSpec = await loadAndBundleSpec(spec || specUrl!);
|
this._resolvedSpec = await loadAndBundleSpec(spec || specUrl!);
|
||||||
|
this.updateStore(this._resolvedSpec, specUrl, options);
|
||||||
|
} catch (e) {
|
||||||
|
this.setState({
|
||||||
|
error: e,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
updateStore(resolvedSpec, specUrl, options) {
|
||||||
|
try {
|
||||||
this.setState({
|
this.setState({
|
||||||
loading: false,
|
loading: false,
|
||||||
store: new AppStore(resolvedSpec, specUrl, options),
|
store: new AppStore(resolvedSpec, specUrl, options),
|
||||||
|
error: undefined,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.setState({
|
this.setState({
|
||||||
|
@ -55,6 +69,16 @@ export class StoreProvider extends Component<StoreProviderProps, StoreProviderSt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentWillReceiveProps(nextProps) {
|
||||||
|
if (this.props.specUrl !== nextProps.specUrl || this.props.spec !== nextProps.spec) {
|
||||||
|
setTimeout(() => this.load(), 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.props.options !== nextProps.options && this._resolvedSpec) {
|
||||||
|
this.updateStore(this._resolvedSpec, nextProps.specUrl, nextProps.options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setError(e?: Error) {
|
setError(e?: Error) {
|
||||||
this.setState({
|
this.setState({
|
||||||
error: e,
|
error: e,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user