Skip to content

Commit 9993458

Browse files
committed
allow updating props
1 parent 7976c17 commit 9993458

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

src/router.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,21 @@ export function Router(props) {
118118
const didSuspend = useRef();
119119
didSuspend.current = false;
120120

121-
cur.current = useMemo(() => {
121+
useMemo(() => {
122122
// This hack prevents Preact from diffing when we swap `cur` to `prev`:
123123
if (this.__v && this.__v.__k) this.__v.__k.reverse();
124124

125125
count.current++;
126-
127126
prev.current = cur.current;
128-
129-
let p, d, m;
130-
toChildArray(props.children).some(vnode => {
131-
const matches = exec(rest, vnode.props.path, (m = { ...vnode.props, path: rest, query, params, rest: '' }));
132-
if (matches) return (p = cloneElement(vnode, m));
133-
if (vnode.props.default) d = cloneElement(vnode, m);
134-
});
135-
136-
return h(RouteContext.Provider, { value: m }, p || d);
137127
}, [url]);
138128

129+
let pr, d, m;
130+
toChildArray(props.children).some(vnode => {
131+
const matches = exec(rest, vnode.props.path, (m = { ...vnode.props, path: rest, query, params, rest: '' }));
132+
if (matches) return (pr = cloneElement(vnode, m));
133+
if (vnode.props.default) d = cloneElement(vnode, m);
134+
});
135+
cur.current = h(RouteContext.Provider, { value: m }, pr || d);
139136
// Reset previous children - if rendering succeeds synchronously, we shouldn't render the previous children.
140137
const p = prev.current;
141138
prev.current = null;

test/router.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,68 @@ describe('Router', () => {
5555
});
5656
});
5757

58+
it.only('should allow updating props in a route', async () => {
59+
const Home = jest.fn(() => html`<h1>Home</h1>`);
60+
const stack = [];
61+
let loc;
62+
render(
63+
html`
64+
<${LocationProvider}>
65+
<${Router}
66+
onRouteChange=${url => {
67+
stack.push(url);
68+
}}
69+
>
70+
<${Home} path="/" test="2" />
71+
<//>
72+
<${() => {
73+
loc = useLocation();
74+
}} />
75+
<//>
76+
`,
77+
scratch
78+
);
79+
80+
expect(scratch).toHaveProperty('textContent', 'Home');
81+
expect(Home).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '', test: '2' }, expect.anything());
82+
expect(loc).toMatchObject({
83+
url: '/',
84+
path: '/',
85+
query: {},
86+
route: expect.any(Function)
87+
});
88+
89+
Home.mockReset();
90+
91+
render(
92+
html`
93+
<${LocationProvider}>
94+
<${Router}
95+
onRouteChange=${url => {
96+
stack.push(url);
97+
}}
98+
>
99+
<${Home} path="/" test="3" />
100+
<//>
101+
<${() => {
102+
loc = useLocation();
103+
}} />
104+
<//>
105+
`,
106+
scratch
107+
);
108+
109+
console.log(scratch.outerHTML)
110+
expect(scratch).toHaveProperty('textContent', 'Home');
111+
expect(Home).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '', test: '3' }, expect.anything());
112+
expect(loc).toMatchObject({
113+
url: '/',
114+
path: '/',
115+
query: {},
116+
route: expect.any(Function)
117+
});
118+
});
119+
58120
it('should switch between synchronous routes', async () => {
59121
const Home = jest.fn(() => html`<h1>Home</h1>`);
60122
const Profiles = jest.fn(() => html`<h1>Profiles</h1>`);

0 commit comments

Comments
 (0)