Skip to content

Commit

Permalink
fix: remove component from DOM on unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
bietkul committed Dec 16, 2022
1 parent 2262913 commit fb18f0d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
10 changes: 5 additions & 5 deletions packages/vue/src/components/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ export default {
validator(store) {
if (!store.dispatch && !store.subscribe && !store.getState) {
throw new Error(
'[reactivesearch-vue] - store provided is not a valid redux store'
'[reactivesearch-vue] - store provided is not a valid redux store',
);
}
return true;
}
},
},
analyticsRef: {
type: Object,
required: true,
}
required: false,
},
},
provide() {
return {
Expand All @@ -29,5 +29,5 @@ export default {
return h('div', this.$slots.default);
}
return this.$slots.default[0];
}
},
};
16 changes: 12 additions & 4 deletions packages/vue/src/components/basic/ComponentWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ const ComponentWrapper = (
},
) => ({
name: 'ComponentWrapper',
$timestamp: null,
props: {
destroyOnUnmount: VueTypes.bool.def(false),
destroyOnUnmount: VueTypes.bool.def(true),
},
created() {
// clone the props for component it is needed because attrs gets changed on time
Expand All @@ -48,6 +49,7 @@ const ComponentWrapper = (
this.componentProps = parsedProps;
this.componentId = this.componentProps.componentId;
this.react = this.componentProps.react;
this.$timestamp = new Date().getTime();
},
beforeMount() {
let components = [];
Expand Down Expand Up @@ -100,10 +102,16 @@ const ComponentWrapper = (
},
beforeDestroy() {
if (this.destroyOnUnmount) {
let registeredComponentsTimestamps = {};
if (this.$$store) {
({ registeredComponentsTimestamps } = this.$$store.getState());
}
// Unregister components
this.removeComponent(this.componentId);
if (this.internalComponent) {
this.removeComponent(this.internalComponent);
if (registeredComponentsTimestamps[this.componentId] === this.$timestamp) {
this.removeComponent(this.componentId);
if (this.internalComponent) {
this.removeComponent(this.internalComponent);
}
}
}
},
Expand Down
15 changes: 12 additions & 3 deletions packages/web/src/components/basic/ComponentWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ComponentWrapper extends React.Component {

constructor(props, context) {
super(props, context);
this._timestamp = new Date().getTime();
// Register a component only when `destroyOnUnmount` is `true`
// or component is not present in store
let components = [];
Expand Down Expand Up @@ -102,9 +103,16 @@ class ComponentWrapper extends React.Component {
// Unregister components
const { componentId, destroyOnUnmount } = this.props;
if (destroyOnUnmount) {
this.props.removeComponent(componentId);
if (this.internalComponent) {
this.props.removeComponent(this.internalComponent);
let registeredComponentsTimestamps = {};
if (this.context && this.context.getState) {
({ registeredComponentsTimestamps } = this.context.getState());
}
// Unregister components
if (registeredComponentsTimestamps[componentId] === this.$timestamp) {
this.props.removeComponent(componentId);
if (this.internalComponent) {
this.props.removeComponent(this.internalComponent);
}
}
}
}
Expand Down Expand Up @@ -155,6 +163,7 @@ ComponentWrapper.propTypes = {

ComponentWrapper.defaultProps = {
setReact: true,
destroyOnUnmount: true,
};

const mapDispatchToProps = (dispatch, ownProps) => ({
Expand Down

0 comments on commit fb18f0d

Please sign in to comment.