"Context is primarily used when some data needs to be accessible by many components at different nesting levels. Apply it sparingly because it makes component reuse more difficult.
If you only want to avoid passing some props through many levels, component composition is often a simpler solution than context.
For example, consider a Page component that passes a user and avatarSize prop several levels down so that deeply nested Link and Avatar components can read it:
It might feel redundant to pass down the user and avatarSize props through many levels if in the end only the Avatar component really needs it. It’s also annoying that whenever the Avatar component needs more props from the top, you have to add them at all the intermediate levels too.
Boa noite!
Olhando a documentação do React na parte de Context, achei o seguinte trecho:
"Context is primarily used when some data needs to be accessible by many components at different nesting levels. Apply it sparingly because it makes component reuse more difficult. If you only want to avoid passing some props through many levels, component composition is often a simpler solution than context.
For example, consider a Page component that passes a user and avatarSize prop several levels down so that deeply nested Link and Avatar components can read it:
It might feel redundant to pass down the user and avatarSize props through many levels if in the end only the Avatar component really needs it. It’s also annoying that whenever the Avatar component needs more props from the top, you have to add them at all the intermediate levels too.
One way to solve this issue without context is to pass down the Avatar component itself so that the intermediate components don’t need to know about the user or avatarSize props:"
Isso significa chamar a função que monta o componente onde teríamos as props que seriam passadas. Ou seja, ao invés de passar as props até chegarem no componente que a usaria, passamos o componente em si onde temos essas props até que ele chegue no seu nó.
Então minhas 2 perguntas:
1) Nesse caso, qual a vantagem de chamar a função que monta o componente ao invés de passar as props (entendo que essas props sejam "greedy", mas pq não acontece o mesmo passando o componente)?
2) Por fim, pelo que entendi, isso melhora a reutilização do componente mesmo no caso do Context. Mas como? Qual a vantagem em termos de reusabilidade e desacoplamento de chamar a função que monte o componente ao invés de usar o Context?
Obrigado!