Closed prkeshri closed 9 months ago
The proposal looks fine but their are certain things here:
Introducing a universal solution for component overrides might make the codebase harder to maintain in the long run. Developers might need to spend extra time understanding and debugging the context-based overrides, especially if the application grows in complexity.
The proposed solution allows for the override of any component, which might lead to unexpected behavior. It breaks the usual contract between components and their consumers, potentially causing issues when different parts of the application rely on consistent component behavior.
React already provides mechanisms for component customization, such as props and higher-order components. These established patterns might be more familiar to developers and could be preferred over introducing a new context-based approach.
let me know your thoughts.
CC @rickhanlonii
It's clever, but dependency injection would violate the declarative principles of React since anything anywhere could override anything. If you need to provide a component to a library, that library should provide a prop, and if they dont then you can fork the library.
Closing as won't do, but thanks for the suggestion.
Hi/ Namaste
In certain scenarios, it becomes necessary to have custom components rendered instead of the given one (say via 3rd party libraries). An example can be an existing library rendering some input component, but we want our custom component to be rendered. So, an example can be:
This component may contain child components and so on, finally leading to render an input like:
Now, one way to have some custom component is to pass some prop via context api and let the
Some_Child_Component_In_Hierarchy
decide the appropriate component to render, Or even pass the component via the context api.But what if the
Some_Child_Component_In_Hierarchy
is a third party who have fixed the input component and we have no way to pass the info? In our custom implementations, we can alter the way we wish, but especially in the above case, it is impossible to do.So, one solution I can think of is to:
Now, any component can provide Override for child components via:
function OverridenOrOriginal({ Orig, props }) { const { name } = Orig; // In case of Custom component, we have name else Orig is a string const { [name ?? Orig]: Overriden } = useContext(ComponentContext); // We may create a custom hook for this if (Overriden) { return original_Jsx(Overriden, props); } else { return original_Jsx(Orig, props); } }