I'm used to export default React.memo(SomeComponent);
so React can memoize my components.
But in a file where I'm using named exports. How can I use React.memo()
?
export {React.memo(MainPostTopic), React.memo(MainPostSubtopic)};
The line above doesn't work.
EXAMPLE FILE
function MainPostTopic(props) {
console.log('Rendering MainPostTopic...');
return(
<LS.Topic_H2 id={props.id}>
{props.value}
</LS.Topic_H2>
);
}
function MainPostSubtopic(props) {
console.log('Rendering MainPostSubtopic...');
return(
<LS.Subtopic_H3>
{props.value}
</LS.Subtopic_H3>
);
}
export {MainPostTopic, MainPostSubtopic};
This is what I've ended up doing:
const MainPostTopic = React.memo(
function MainPostTopic(props) {
console.log('Rendering MainPostTopic...');
return(
...
);
}
);
const MainPostSubtopic= React.memo(
function MainPostSubtopic(props) {
console.log('Rendering MainPostSubtopic...');
return(
...
);
}
);
export {MainPostTopic, MainPostSubtopic};
I kept the same name for the functions and the exports.
Try
export const MemoMainPostTopic = React.memo(MainPostTopic);
or
const MainPostTopic = memo(() => {
...
})
export { MainPostTopic };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With