Using React In my below code I have two button when I click on button1 then open another URL which is fetched in API https://mocki.io/v1/be3cb19b-bd49-4a82-b19b-44b859e19d5d based on id1 and when I click on button then then open different URL which is fetched in API which id 2 based on id fetched.
How can we do that using React?
https://codesandbox.io/s/challenge-7-fetch-a-list-final-forked-5me46?file=/src/index.js check here in my code I am not able to do that when I click on button1 then URL not fetched in API and not open. I'm stuck on that.
import React, { useState } from "react";
import ReactDOM from "react-dom";
import axios from "axios";
import ScotchInfoBar from "./ScotchInfoBar";
import "./styles.css";
function App() {
const fetchData = async () => {
const response = await axios.get(
"https://mocki.io/v1/4d51be0b-4add-4108-8c30-df4d60e8df54"
);
response.data;
};
return (
<div className="App">
{/* Fetch data from API */}
<div>
<button className="fetch-button" onClick={fetchData}>
Data1
</button>
<button className="fetch-button" onClick={fetchData}>
Data2
</button>
<br />
</div>
{/* Display data from API */}
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);