-4

I have a situation in which I have to get data from child to parent. But without using state of parent component. I want to directly access the data of child component directly in the render method of parent component.

karan patel
  • 61
  • 1
  • 7
  • Why can't you pass the data from the parent to the child? Is it loaded in the child or state dependent? – RayWiis Oct 07 '19 at 13:47
  • Possible duplicate of [How to pass data from child component to its parent in ReactJS?](https://stackoverflow.com/questions/38394015/how-to-pass-data-from-child-component-to-its-parent-in-reactjs) – Diogo Peres Oct 07 '19 at 13:47
  • @RayWiis I have a filtering logic in render method of parent and also this child component is inside render method, after filtering logic. I get the data from child component to the parent component, if I use state then, entire logic will rerender and againg same loop will run – karan patel Oct 07 '19 at 13:50

2 Answers2

1

For your case, move the filtering logic outside of the render method then save what you need to show into state and render from state. You can use React's component lifecycle methods to filter whatever you want to show and update the state. https://reactjs.org/docs/state-and-lifecycle.html

RayWiis
  • 140
  • 6
0

If you are set on not using state in the parent component, the only way I can think to do this is using refs. I wouldn't recommend using refs when the parent state will do, but if your use case prevents that for whatever reason here is the link to the docs https://reactjs.org/docs/refs-and-the-dom.html.

Brian Thompson
  • 13,263
  • 4
  • 23
  • 43