Im working on a ReactJS project where you can generate a random activity when pressing the generate button. Im doing a fetch to my api to display some data from it. This works fine. Now I want to do the fetch and display the returned data every time I click the generate button (which is in the home component). So the generate button has to do a new fetch and refresh the component. Also it has to be empty on default. I searched a while and can't find a relevant answer. Can some of you guys help me out? Thanks.
fetch component
import React from "react";
export class ItemLister extends React.Component {
constructor() {
super();
this.state = { suggestion: "" };
}
componentDidMount() {
fetch("......./suggestions/random", {
method: "GET",
dataType: "JSON",
headers: {
"Content-Type": "application/json; charset=utf-8"
}
})
.then(resp => {
return resp.json();
})
.then(data => {
this.setState({ suggestion: data.suggestion });
})
.catch(error => {
console.log(error, "catch the hoop");
});
}
render() {
return <h2>{this.state.suggestion}</h2>;
}
}
home component
import React from "react";
import { ItemLister } from "./apiCall";
export class Home extends React.Component {
constructor(props) {
super(props);
console.log();
window.sessionStorage.setItem("name", this.props.params.name);
window.sessionStorage.setItem("token", this.props.params.token);
}
render() {
return (
<div className="contentContainer AD">
<table>
<thead>
<tr>
<th>
<p>Het is heel leuk als het werkt!</p>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<ItemLister />
</td>
</tr>
</tbody>
</table>
<div className="container center">
<button>GENERATE</button>
<button>SAVE</button>
<button>MATCH</button>
</div>
</div>
);
}
}