Skip to content

Commit e3b37f7

Browse files
committed
fix: components were recreated with initial values of their props, instead of last (currnet) values
1 parent f7ef490 commit e3b37f7

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

runtime/svelte-hooks.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@ const captureState = cmp => {
1919
if (!cmp.$$) {
2020
throw new Error('Invalid component')
2121
}
22+
2223
const {
2324
$$: { callbacks, bound, ctx },
2425
} = cmp
26+
2527
const state = cmp.$capture_state()
26-
return { ctx, callbacks, bound, state }
28+
29+
// capturing current value of props (or we'll recreate the component with the
30+
// initial prop values, that may have changed -- and would not be reflected in
31+
// options.props)
32+
const props = Object.assign({}, cmp.$$.props)
33+
Object.keys(cmp.$$.props).forEach(prop => {
34+
if (state.hasOwnProperty(prop)) {
35+
props[prop] = state[prop]
36+
}
37+
})
38+
39+
return { ctx, callbacks, bound, state, props }
2740
}
2841

2942
// restoreState
@@ -83,6 +96,21 @@ export const createProxiedComponent = (
8396

8497
const assignOptions = (target, anchor, restore, preserveLocalState) => {
8598
const props = Object.assign({}, options.props)
99+
100+
// Filtering props to avoid "unexpected prop" warning
101+
// NOTE this is based on props present in initial options, but it should
102+
// always works, because props that are passed from the parent can't
103+
// change without a code change to the parent itself -- hence, the
104+
// child component will be fully recreated, and initial options should
105+
// always represent props that are currnetly passed by the parent
106+
if (options.props && restore.props) {
107+
for (const prop of Object.keys(options.props)) {
108+
if (restore.props.hasOwnProperty(prop)) {
109+
props[prop] = restore.props[prop]
110+
}
111+
}
112+
}
113+
86114
if (preserveLocalState && restore.state) {
87115
if (Array.isArray(preserveLocalState)) {
88116
// form ['a', 'b'] => preserve only 'a' and 'b'

0 commit comments

Comments
 (0)