mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-22 08:36:33 +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 { RedocRawOptions } from '../services/RedocNormalizedOptions';
|
||||
import { loadAndBundleSpec } from '../utils';
|
||||
import { OpenAPISpec } from '../types';
|
||||
|
||||
interface StoreProviderProps {
|
||||
specUrl?: string;
|
||||
|
@ -23,6 +24,8 @@ interface StoreProviderState {
|
|||
export class StoreProvider extends Component<StoreProviderProps, StoreProviderState> {
|
||||
store: AppStore;
|
||||
|
||||
private _resolvedSpec: OpenAPISpec;
|
||||
|
||||
constructor(props: StoreProviderProps) {
|
||||
super(props);
|
||||
|
||||
|
@ -43,10 +46,21 @@ export class StoreProvider extends Component<StoreProviderProps, StoreProviderSt
|
|||
});
|
||||
|
||||
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({
|
||||
loading: false,
|
||||
store: new AppStore(resolvedSpec, specUrl, options),
|
||||
error: undefined,
|
||||
});
|
||||
} catch (e) {
|
||||
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) {
|
||||
this.setState({
|
||||
error: e,
|
||||
|
|
Loading…
Reference in New Issue
Block a user