Skip to content

Commit

Permalink
fix: don't emit changes when the helmetData wrapper object changes
Browse files Browse the repository at this point in the history
This makes the context free enviornment behave like a context environment,
for example, the outer context could be destroyed and recreated, and emiting
changes isn't effected. The helmetData should behave the same.
  • Loading branch information
blittle authored and staylor committed Dec 6, 2021
1 parent 906b53d commit 6e46a51
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
5 changes: 2 additions & 3 deletions __tests__/server/helmetData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,13 @@ describe('Helmet Data', () => {

it('works with the same context object but separate HelmetData instances', () => {
const context = {};
const instances = [];

render(
<div>
<Helmet helmetData={new HelmetData(context, instances)}>
<Helmet helmetData={new HelmetData(context)}>
<base href="http://mysite.com" />
</Helmet>
<Helmet helmetData={new HelmetData(context, instances)}>
<Helmet helmetData={new HelmetData(context)}>
<base href="http://mysite.com/public" />
</Helmet>
</div>
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ declare module 'react-helmet-async' {
}

export class HelmetData {
constructor(context: any, instances?: Array<any>)
constructor(context: any)
}

export class HelmetProvider extends React.Component<ProviderProps> {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import invariant from 'invariant';
import { Context } from './Provider';
import HelmetData from './HelmetData';
import Dispatcher from './Dispatcher';
import { without } from './utils';
import { TAG_NAMES, VALID_TAG_NAMES, HTML_TAG_MAP } from './constants';

export { default as HelmetData } from './HelmetData';
export { default as HelmetProvider } from './Provider';

/* eslint-disable class-methods-use-this */

export class Helmet extends Component {
/**
* @param {Object} base: {"target": "_blank", "href": "http://mysite.com/"}
Expand Down Expand Up @@ -63,7 +63,7 @@ export class Helmet extends Component {
static displayName = 'Helmet';

shouldComponentUpdate(nextProps) {
return !fastCompare(this.props, nextProps);
return !fastCompare(without(this.props, 'helmetData'), without(nextProps, 'helmetData'));
}

mapNestedChildrenToProps(child, nestedChildren) {
Expand Down Expand Up @@ -235,7 +235,7 @@ export class Helmet extends Component {

return helmetData ? (
// eslint-disable-next-line react/jsx-props-no-spreading
<Dispatcher {...newProps} context={helmetData.value} />
<Dispatcher {...newProps} context={helmetData.value} helmetData={undefined} />
) : (
<Context.Consumer>
{(
Expand Down
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,10 @@ export const prioritizer = (elementsList, propsToMatch) => {
}
return { default: elementsList };
};

export const without = (obj, key) => {
return {
...obj,
[key]: undefined,
};
};

0 comments on commit 6e46a51

Please sign in to comment.