Reputation:
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>
Upvotes: 0
Views: 59
Reputation: 1485
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.
Upvotes: 2