0

I have this component

const BoolComponent = ({ field1, Component1, Component2}) => {
  //...
  flag = functionGetBool(field1)
  return flag ? Component1 : Component2
}

When I use it, is there any way to get the flag variable in my component? how can i get the answer's value?

<BoolComponent here i want to get the flag> </BoolComponent>

also here over:

<BoolComponent >
here i want to get the flag

 </BoolComponent>
  • 1
    It's totally unclear what/if there is a problem and what the question even is. Can you more clearly state what you are trying to do and what isn't working? – Drew Reese Jun 25 '20 at 04:18
  • `` This is the place you send prop. So you can try `` and in component, use it to return necessary component – Rajesh Jun 25 '20 at 04:26
  • Does this answer your question? [How to access method of the child component from parent in reactjs](https://stackoverflow.com/questions/57955390/how-to-access-method-of-the-child-component-from-parent-in-reactjs) – aravind_reddy Jun 25 '20 at 04:32

1 Answers1

2

State Lifting sounds to be a great option in such cases. You can evaluate functionGetBool in parent component itself and pass it down as state in BoolComponent.

For reference: https://reactjs.org/docs/lifting-state-up.html

Use Ref

Secondly you can make BoolComponent a class component and make one function in it like

export function GetBool(){
 let flag = functionGetBool(field1)
return flag;
}

In your parent component you can use ref of BoolComponent and access the function GetBool() to know the value.

Raghvender Kataria
  • 1,457
  • 7
  • 14