ppowell777
Well-Known Coder
I am trying to write a very simple React standalone function that will take a URL as a parameter and return the fetched HTML as a result to the parent component.
Here is my code:
And here is the Header.js that calls it (among other React components that will also call it):
I have no idea how to fix this issue; this after hours of Google searching to no avail. Please help.
Thanks
Here is my code:
JavaScript:
import { useEffect, useState } from 'react';
const GetData = ({ props }) => {
const [html, setHtml] = useState('');
const [loading, setLoading] = useState(true);
useEffect((props) => {
console.log(typeof props); // "undefined" why?
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, 5000);
fetch(props.url, {
signal: controller.signal
})
.then(response => {
clearTimeout(timeout);
if (response.ok && response.status === 200) {
return response.text();
} else if (response.status >= 400) {
return Promise.reject(response.statusText);
}
})
.then(async html => {
setHtml(await html);
setLoading(false);
})
.catch(error => {
console.error(error);
});
});
if (loading) {
return '';
} else {
return html;
}
}
export default GetData;
And here is the Header.js that calls it (among other React components that will also call it):
JavaScript:
import React from 'react';
import GetData from './get_data';
import headerPng from './images/header.png';
const Header = () => {
const myUrl = 'http://localhost/test.php';
return (
<header id="fvap_header" className="usa-header">
<div><GetData url={ myUrl } /></div>
This is my header
</header>
);
};
export default Header;
I am passing the URL as I understand how to pass a parameter to a React component, and yet I constantly get the error:
Cannot read properties of undefined: 'props"
I have no idea how to fix this issue; this after hours of Google searching to no avail. Please help.
Thanks