I have a father component and I import 3 child components in it. All I want is to execute a function and change the "isToggleOn" variable.
import React, { Component } from 'react'
import Child1 from './Child1'
import Child2 from './Child2'
import Child3 from './Child3'
export default class Father extends Component {
constructor(props) {
super(props);
this.state = {
isToggleOn: false,
isLoggedIn: null
};
this.FiWantToCall = this.FiWantToCall.bind(this);
}
FiWantToCall = () => {
this.setState(prevState => ({
isToggleOn: !prevState.isToggleOn
}));
}
render() {
return (
<div className="a-class">
<Child1 />
<div>
{this.state.isLoggedIn ? (
<Child2 />
) : (
<Child3 />
)}
</div>
</div>
);
}
}
And in every Child component let's say I have a:
<button onClick={this.FiWantToCall}>Button</button>
I don't know if calling this.FiWantToCall is right but I wrote it just to explain what I want. Is it possible to call a function from Child written in Father?