0

I have this in my Child Component. I want to use showTab state in its parent component. How do i send it to its parent?

let enable = false;

if (value) {
            enable = true;
        }

        this.state = {
            showTab: enable
        };

 handleChangeTab = ()=>{
        this.setState({
            showTab: !this.state.showTab
        });
    }
komal
  • 43
  • 6
  • You can't send props to parent but you can send a callback to the child from the parent which child can use to call the parent – innocent May 24 '22 at 06:07
  • The easiest way would be for [the parent to manage the state](https://reactjs.org/docs/lifting-state-up.html), and pass that down to the child component along with a handler that the child component can call with a new value. – Andy May 24 '22 at 06:08

1 Answers1

0

You need to Lift State Up:

constructor(props) {
  super(props);
  this.handleChangeTab = this.handleChangeTab.bind(this);
  let enable = false;
}

if (value) enable = true;

this.state = { showTab: enable };

handleChangeTab = () => {
  this.setState({showTab: !this.state.showTab});
}
Akira Taguchi
  • 111
  • 1
  • 7