Skip to content

Commit

Permalink
Re-render when components prop changes
Browse files Browse the repository at this point in the history
  • Loading branch information
d3x7r0 committed Jul 10, 2019
1 parent eba6df8 commit 0c07b72
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"npm-run-all": "^4.1.5",
"preact": "^8.4.2",
"preact-jsx-chai": "^2.2.1",
"preact-render-spy": "1.2.1",
"rollup": "^1.16.7",
"sinon": "^7.3.2",
"sinon-chai": "^3.3.0",
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ export default class Markup extends Component {
customReviver = h;
}

shouldComponentUpdate({ wrap, type, markup }) {
let p = this.props;
shouldComponentUpdate({ wrap, type, markup, components }) {
const p = this.props;
if (Object.keys(components).join()!==Object.keys(p.components).join()) {
return true;
}
for (let i in Object(components)) {
if (components[i]!==p.components[i]) {
return true;
}
}
return wrap!==p.wrap || type!==p.type || markup!==p.markup;
}

Expand Down
29 changes: 29 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { h, render, rerender, Component } from 'preact';
import deep from 'preact-render-spy';
import assertJsx from 'preact-jsx-chai';
import sinonChai from 'sinon-chai';
chai.use(assertJsx);
Expand Down Expand Up @@ -200,6 +201,34 @@ describe('Markup', () => {
);
});

it('should update when mapped components change', () => {
const XFoo = () =>
(<div class="x-foo">hello foo</div>);
const XBar = () =>
(<div class="x-bar">hello bar</div>);

let context = deep(
<Markup type="html" markup='<x-component />' components={{ XComponent: XFoo }} />,
{ depth: Infinity }
);

expect(context.output()).to.eql(
<div class="markup">
<div class="x-foo">hello foo</div>
</div>
);

context.render(
<Markup type="html" markup='<x-component />' components={{ XComponent: XBar }} />
);

expect(context.output()).to.eql(
<div class="markup">
<div class="x-bar">hello bar</div>
</div>
);
});

describe('allow-scripts option', () => {
before( () => {
window.stub = sinon.stub();
Expand Down

0 comments on commit 0c07b72

Please sign in to comment.