Passing props to a wrapped component #205
-
Hi, I have a wrapper component that presents a form in a modal, The wrapped form component is necessarily asynchronous because form fields are populated via asynchronous calls. But I'm having problems passing props down to the wrapped component. How can I get the wrapped component to refresh with new props? Thanks in advance. A simplified equivalent version looks like this: function Wrapper(Component) {
return function* (props) {
while (true)
yield <Component {...props} />;
};
};
async function* Wrapped({count}) {
for await (const _ of this) {
console.log('counting in Wrapped Component', props.count); // does not refresh
yield <div>My count is {count}</div>;
};
};
const MyComponent = Wrapper(Wrapped);
function* App() {
let seconds = 0;
const interval = setInterval(() => {
seconds++;
this.refresh();
}, 1000);
try {count
while (true) {
console.log('counting in App', seconds); // counts
yield (
<MyComponent count={seconds} />
);
}
} finally {
clearInterval(interval);
}
}; Ngā mihi, Darryl. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
It looks like both in the reproduction and in your actual code, you’re not updating the props to latest in either the wrapper or wrapped function. function wrap(Component) {
return function *(props) {
for (props of this)
yield <Component {...props} />;
};
}
async function *Wrapped({count}) {
for await ({count} of this) {
console.log('counting in Wrapped Component', count); // does not refresh
yield <div>My count is {count}</div>;
};
}
const MyComponent = wrap(Wrapped);
function *App() {
let seconds = 0;
const interval = setInterval(() => {
seconds++;
this.refresh();
}, 1000);
try {
for ({} of this) {
console.log('counting in App', seconds); // counts
yield (
<MyComponent count={seconds} />
);
}
} finally {
clearInterval(interval);
}
} Anywhere you’re using We might want a linter rule at some point to warn users about this. I can imagine it can be very confusing! Kia ora! |
Beta Was this translation helpful? Give feedback.
It looks like both in the reproduction and in your actual code, you’re not updating the props to latest in either the wrapper or wrapped function.