Functional Components context.parent is root instance
See original GitHub issueLet’s say I have the following template:
<div id="app">
<parent-component>
<functional-child-component />
<normal-child-component />
</ parent-component>
</div>
If I use this.$parent
inside the normal-child-component
I get access to the parent-component
, as expected.
If I use the same logic inside a functional child component, however, the behavior is different. context.parent
gives me access to the root instance, similar to if I was using this.$root
in a normal component. Is this the intended behavior? If it is, how can I get access to actual $parent
instance inside a functional child component?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Is $root accessible from functional component context?
Is there a reference available to $root inside a functional component's render function? I know that parent is available, but I'm unsure parent...
Read more >Context - React
Context.Consumer ... A React component that subscribes to context changes. Using this component lets you subscribe to a context within a function component....
Read more >is there any way to access the parent component instance in ...
Using context (if there's no direct parent/child relation) class Parent extends React.Component { constructor(props) { super(props) this.fn ...
Read more >React: Utilizing Context API With Class And Functional ...
The article involves the use case of context API in React. Here you will learn how you can use context API with class...
Read more >How to use React Context API with Functional | Class ... - CronJ
Below is an example of the above scenario. React Context. Level 1 – Parent Component (App.js). Level ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
To be honest, I think making a component behave differently based on the parent it is inserted into sounds like a bad idea to me - the child component is now coupled to the implementation details to all possible components it may be nested inside. I’d recommend rethinking the design and adhere to the “props down, events up” pattern.
Also, you don’t have to make it functional to use render functions. You can use render functions with normal components too.
Another example:
Using functional components to render the pages, buttons and alike is a good way to let the developer choose the composition of the pagination while not sacrificing performance. Still, the inner components rely on the parent to fire change events or render details (pagination props).
Convertir
vk-pagination-pages
into a normal componente can’t even be done as it doesn’t support rendering multiple roots.