I have a function that returns an object containing a component and I am passing props from my function to the component but I see the props coming as an empty object.
export function withBackgroundRed<TProps = any>(
Component: any,
props?: TProps
) {
console.log('props', props);
return {
Wrapper: function Wrapper(p = props) {
console.log('p', p);
return (
<div style={{ backgroundColor: 'red' }}>
<Component {...p} />
</div>
);
},
};
}
function App(props) {
return <div>props: {JSON.stringify(props)}</div>;
}
const WithSomeContext = withBackgroundRed(App, { props1: 'props1' }).Wrapper;
export default WithSomeContext;
The first. console.log() print the props as expected, but the second one is just an empty object.
2
Answers
That would be because React will always pass an object with the live props to a component you render.
So the default value you use for
p
will never be used, as it will receive the props used (an empty object if no props where passed when rendering the wrapped component).You should merge the initial props with the ones provided to to the wrapped component.
Something like
You can swap the
props, instanceProps
in theObject.assign
depending on which ones you want to prevail in case of conflict.Demo: https://stackblitz.com/edit/stackblitz-starters-rlxcy7
There is a more convenient way to do this kind of wrapper: