I have a function that is used to change the state of a react component but I'm trying to pass the function in another file. I get the error that the function I'm trying to pass (changeView) is not defined.
This is the App.js
export default class App extends Component { constructor() { super(); this.state = { language: "english", render: '' } } changeView(view, e){ console.log(view); this.setState({render: view}); } _renderSubComp(){ switch(this.state.render){ case 'overview': return <Overview /> case 'reviews': return <Reviews /> } } render() { const {render} = this.state return <Fragment> <Header language={this.state.language} /> <Hero /> <Navigation render={render}/> {this._renderSubComp()} </Fragment>; } }
I'm trying to pass the changeView method to the Navigation.JS component, so I can change the active link as well as render the components listed in the _renderSubComp method above.
import React from "react"; import "./navigation.css"; import { changeView } from "../app"; export default function Navigation() { return <div className="navigation"> <a onClick={this.changeView.bind(this, 'overview')}>Overview</a> <a>Reviews</a> </div>; } How should I pass the function to another file so it's able to change the state of my component and render the component I need.