I want the results of an api call to be shown on an '/events' page. I've tried using Redirect from react-router-dom but this only redirects the page.
I have tried looking at this answer
How can I on Submit my Form redirect the Results to another Page in React
But am unsure how this could apply to my code. If I use this.props.history.push('/events') I get an error message saying it doesn't recognize .push.
Would be grateful if someone can put me on the right course.
import React, {Component} from 'react';
import axios from 'axios';
import {Form, FormControl, Button} from 'react-bootstrap';
import './style.css';
class SearchField extends Component {
state = {
search: ""
};
handleChange = (event) => {
const {name, value} = event.target;
this.setState({[name]: value.toLowerCase()});
};
apiCall = () =>{
const corsAnywhere = "https://cors-anywhere.herokuapp.com/";
const ticketmasterURL = "https://app.ticketmaster.com/discovery/v2/events/?keyword=";
const searchKey = process.env.REACT_APP_TM_KEY;
const term = this.state.search.split(" ").join("+");
axios.get(corsAnywhere + ticketmasterURL + term + "&apikey=" + searchKey)
.then(res => {
console.log(res.data._embedded.events);
this.history.push("/events");
})
.catch(err => console.log(err));
};
handleSubmit = (event) => {
event.preventDefault();
this.apiCall();
};
render(){
return (
<div className="search-container">
<Form onSubmit={this.handleSubmit}>
<Form.Group>
<FormControl
type="text"
placeholder="Search"
name="search"
value={this.state.search}
onChange={this.handleChange}
/>
<div className="btn-container">
<Button type="submit">Submit</Button>
</div>
</Form.Group>
</Form>
</div>
)
}
}
export default SearchField;