question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Functional Components context.parent is root instance

See original GitHub issue

Let’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:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
yyx990803commented, Dec 23, 2016

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.

0reactions
miljan-aleksiccommented, Feb 5, 2017

Another example:

<vk-pagination :total="items.length" :active="pagination.active" :limit="pagination.limit">
  <vk-pagination-prev />
  <vk-pagination-pages />
  <vk-pagination-next />
</vk-pagination>

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found