Skip to content

Commit 8a3077f

Browse files
toraoramarvinhagemeister
authored andcommitted
Wrap arrays in Fragment to allow proper rendering (#96)
1 parent a102ba8 commit 8a3077f

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { encodeEntities, indent, isLargeString, styleObjToCss, assign, getChildren } from './util';
22
import { ENABLE_PRETTY } from '../env';
3-
import { options, Fragment } from 'preact';
3+
import { options, Fragment, createElement } from 'preact';
44

55
const SHALLOW = { shallow: true };
66

@@ -39,6 +39,11 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
3939
return '';
4040
}
4141

42+
// wrap array nodes in Fragment
43+
if (Array.isArray(vnode)) {
44+
vnode = createElement(Fragment, null, vnode);
45+
}
46+
4247
let nodeName = vnode.type,
4348
props = vnode.props,
4449
isComponent = false;
@@ -71,7 +76,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
7176
}
7277
else {
7378
let rendered;
74-
79+
7580
let c = vnode.__c = { __v: vnode, context, props: vnode.props };
7681
if (options.render) options.render(vnode);
7782

@@ -98,7 +103,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
98103
else if (c.componentWillMount) c.componentWillMount();
99104
rendered = c.render(c.props, c.state, c.context);
100105
}
101-
106+
102107
if (c.getChildContext) {
103108
context = assign(assign({}, context), c.getChildContext());
104109
}

test/render.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,23 @@ describe('render', () => {
710710
expect(res).to.equal('<div>bar</div>');
711711
});
712712

713+
it('should work with useContext + custom value with multiple children', () => {
714+
let Ctx = createContext('foo');
715+
function Foo() {
716+
let v = useContext(Ctx);
717+
return <div>{v}</div>;
718+
}
719+
720+
let res = render(
721+
<Ctx.Provider value="bar">
722+
<Foo />
723+
<Foo />
724+
</Ctx.Provider>
725+
);
726+
727+
expect(res).to.equal('<div>bar</div><div>bar</div>');
728+
});
729+
713730
it('should work with useState', () => {
714731
function Foo() {
715732
let [v] = useState(0);

0 commit comments

Comments
 (0)