function A() {
return <HeavyComponent someProp={true} />;
}
function B() {
return <HeavyComponent someProp={false} />;
}
function C() {
const [state, setState] = useState(true);
return state ? <A /> : <B />;
}
function D() {
const [state, setState] = useState(true);
return state ? A() : B();
}
As you can see, A and B do very simple jobs.
When state in C changes from true to false, react will remove the element which A has rendered and mount the new one B render. This causes HeavyComponent render from scratch.
If I use D instead of C, the result will be exactly the same. However. The rendering will be more efficient since D will update HeavyComponent rather than remove-and-render-new-one.
The problem is that I want to export both A and B in a library and it's hard to expect other programmers to use A and B in an efficient way like in D.
I think you can point out that I don't need to define A and B. But it's just a simplified example. In real practice, I have more than one props and also more components. Each component represents the semantic of the combination of props passing to HeavyComponent by name of itself. I'll also export HeavyComponent directly, but the user will get confused about its props at the first time. So it would be better export handy components A and B.
Is there any way to make A and B work as in D, even if the programmer use them as in C?