just wondering how I can fix this? I heard about using consumers
but I don't think this will resolve the issue, since I need to call setSaveEnabled
in the handleChange()
function, which cannot be defined inside of a consumer if I were to make one. What other paths is there which I can take?
I call handleChange()
inside of the onchange
arguement in the switch component.
import SaveContext from "../../context/SaveContext";
class EnableSwitch extends Component {
constructor() {
super();
this.state = { checked: false };
this.handleChange = this.handleChange.bind(this);
}
handleChange(checked) {
this.setState({ checked });
const { setSaveEnabled } = useContext(SaveContext);
setSaveEnabled(false);
}
render() {
return (
<div>
<Switch
checked={this.state.checked}
onChange={() => {
this.handleChange();
}}
/>
</div>
);
}
}
React Hook "useContext" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function