Search code examples
javascriptreactjsreact-functional-component

How do I pass a boolean value from my React child to parent component


In my React child component I have access to a variable options which returns an array of objects.

In this component I have a function which checks if there are options:

const hasOptions = () => options.length > 0;

console.log(hasOptions());

This returns true or false.

In my parent component I need this boolean value to conditionally add some styling (based on the boolean value).

How do I pass this boolean value from my child to parent component?


Solution

  • You can create a state variable in the parent component, and then pass the setXXX method to the child component, in the child component call the setXXX method to pass the boolean variable to the parent.