From my landing page I am sending function to my search bar page.
LandingPage.js
export default class LandingPage extends React.Component {
onSearchSubmit(term){
console.log(term);
}
render() {
return (
<div>
<div><SearchBar onSubmit = {this.onSearchSubmit} /></div>
</div>
......
SearchBar.js
export default class SearchBar extends React.Component{
state = {term: ''};
onFormSubmit = event => {
event.preventDefault();
this.props.onSubmit(this.state.term);
};
render(){
return(
<div>
<div className="container">
<div className="searchBar mt-5">
<form onSubmit={this.onFormSubmit}>
<div className="form-group">
<label for="search">Image search</label>
<input
type="text"
className="form-control"
placeholder="Search..."
value = {this.state.term}
onChange={e => this.setState({term: e.target.value})}
/>
</div>
</form>
</div>
</div>
</div>
)
}
}
My landing page (in this case parent) is on http://localhost:3000/
and when I type something in search bar on landing page it works just fine.
But when I go to http://localhost:3000/searchbar
and type there I get following error
Uncaught TypeError: _this.props.onSubmit is not a function
UPDATE:
I have tried with adding binding and it's still not working. It works if I search the search bar on landing page like this.
But my question is if I go to only search bar page which is on http://localhost:3000/searchbar
separate of landing page which is on http://localhost:3000/
can I send data back?
I am still getting same error.
My question is is this even possible with ReactJS to send data back to parent page from child page on different URL?