I know this question is asked a lot because I have done the first two pages of Google and I have searched about it on SO but I didn't found solution for me and I have no idea why i got this error.
So here is my parent component
class App extends Component {
state = { lang: language }
constructor(props) {
super(props)
this.handleLanguage = this.handleLanguage.bind(this)
}
handleLanguage(langValue){
this.setState({lang: langValue});
}
render() {
return (
<IntlProvider locale={this.lang} messages={messages}>
<div className="App">
<div className="App-content">
<React.Fragment>
<div className="menu">
<Navbar handleLanguage={this.handleLanguage}></Navbar>
</div>
<Main />
</React.Fragment>
</div>
</div>
</IntlProvider>
);
}
}
export default (App);
and here is my children component
export class Navbar extends React.Component {
constructor() {
super();
this.langChange = this.langChange.bind(this)
}
langChange(value){
var lang = value;
this.props.handleLanguage(lang);
}
render() {
return (
<nav className="navbar navbar-expand-lg nav-bar-bg">
<a href="/">
<img src={Logo_horizontal_flat} className="logo-horizontal-flat"></img>
</a>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<div className="navbar-nav">
<a className="nav-item nav-link" onClick={() => this.langChange("fr")}><img className="flag" src={flag_fr}></img></a>
<a className="nav-item nav-link" onClick={() => this.langChange("en")}><img className="flag" src={flag_us}></img></a>
</div>
</div>
</nav>
)
}
}
I got this error
× TypeError: this.props.handleLanguage is not a function
I'm trying to pass a data to a parent from his child with following this issue: How to pass data from child component to its parent in ReactJS?
But I have no idea why I still got this error, I even try to Type my props with PropTypes like this
Navbar.propTypes = {
handleLanguage: PropTypes.func
};
but it didn't change anything ...
EDIT
Actually it's working when I remove my Main
components which is a component with my routes
export class Main extends React.Component {
render (){
return (
<Switch>
<Route exact path="/" component={Home}></Route>
<Route path='/signup' component={RegisterPart2} ></Route>
<Route path='/signup1' component={RegisterPart1} ></Route>
<Route path='/signup2' component={RegisterPart3} ></Route>
<Route path='/login' component={Login} ></Route>
<Route path='/profile' component={Profile} ></Route>
<Route path='/information' component={Information} ></Route>
<Route path='/account' component={Account} ></Route>
<Route path='/address' component={Adresses} ></Route>
<Route path='/addadress' component={Addadresses} ></Route>
<Route path='/countries' component={Countries} ></Route>
</Switch>
);
}
}
I have no idea how this component can affected it, any idea ?
EDIT 2
Finally I got the answer, it's because in my Home component I called the Navbar component again.
Thanks for your help everyone
Thank in advance for your help