Goal: I am trying to change the link inside the fetch() function with the value that comes from the dropdown.
Attempts:
1) I attempted passing the value to the function like GetAPILinks(Link_from_dropdown) and calling it in the onSelect function however I am getting an error stating: Invalid hook call. Hooks can only be called inside of the body of a function component
2) I have also tried other methods of passing it by storing it in a variable inside GetAPILinks but resulted in errors.
I am starting to believe I am approaching this all wrong. Any advice or help would be great!
My Code:
This code will fetch the data from a hard coded link and renders a dropdown button that prints the selected values to the console.
import React from 'react';
import { DropdownButton, Dropdown} from 'react-bootstrap';
const GetAPILinks = () => {
const [apiData, setApiData] = React.useState([]);
React.useEffect(() => {
fetch('https://Random_API/Link_From_Dropdown_Goes_Here')
.then((response) => {
return response.json();
})
.then((data) => {
setApiData(data);
});
}, []);
return (
<DropdownButton id="dropdown-item-button" title="API Links" onSelect={function(evt){console.log(evt)}}>
<Dropdown.Item as="button" eventKey='API_Link1'>Link 1</Dropdown.Item>
<Dropdown.Item as="button" eventKey='API_Link2'>Link 2</Dropdown.Item>
<Dropdown.Item as="button" eventKey='API_Link3'>Link 3</Dropdown.Item>
</DropdownButton>
);
}
export default GetAPILinks;