-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update the code to use components for loading the image
- Loading branch information
1 parent
ee4206b
commit 90f9e76
Showing
6 changed files
with
72 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React, { useState } from 'react'; | ||
|
||
const ImageComponent = () => { | ||
|
||
return ( | ||
<div> | ||
Inky Pinky Ponky<br/> | ||
Daddy bought a donkey<br/> | ||
Donkey died<br/> | ||
Daddy cried<br/> | ||
Inky Pinky Ponky<br/> | ||
<img src='./images/donkey.jpg'/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ImageComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React, { useState } from 'react'; | ||
|
||
const DataFetchComponent = () => { | ||
const [apiData, setApiData] = useState(''); | ||
|
||
const fetchData = async () => { | ||
|
||
fetch('https://fd-sandbox.hakabo.com/api/data', { | ||
method: 'GET', | ||
headers: { | ||
'Ocp-Apim-Subscription-Key': '1193b86901a44fa181cb016bfd85089f' | ||
}, | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
setApiData(data); | ||
}) | ||
.catch(error => console.error('Error fetching data:', error)); | ||
|
||
}; | ||
|
||
return ( | ||
<div> | ||
<button onClick={fetchData}>GO!</button> | ||
{apiData && <p>{apiData}</p>} | ||
</div> | ||
); | ||
}; | ||
|
||
export default DataFetchComponent; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,21 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import {createRoot} from 'react-dom/client' | ||
import DonkeyComponent from './donkey'; | ||
import DataFetchComponent from './go'; // Import the new compon | ||
// class Index extends React.Component { | ||
// constructor() { | ||
// super(); | ||
// this.state = { | ||
// data: null, | ||
// }; | ||
// } | ||
|
||
class Index extends React.Component { | ||
constructor() { | ||
super(); | ||
this.state = { | ||
data: null, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
// Make API call with an extra header | ||
fetch('https://fd-sandbox.hakabo.com/api/data', { | ||
method: 'GET', | ||
headers: { | ||
'Ocp-Apim-Subscription-Key': '1193b86901a44fa181cb016bfd85089f' | ||
}, | ||
}) | ||
.then(response => response.json()) | ||
.then(data => this.setState({ data })) | ||
.catch(error => console.error('Error fetching data:', error)); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<h1>Hello, React!</h1> | ||
{this.state.data ? ( | ||
<div> | ||
<p>Data from API:</p> | ||
<pre>{JSON.stringify(this.state.data, null, 2)}</pre> | ||
</div> | ||
) : ( | ||
<p>Loading data...</p> | ||
)} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
ReactDOM.render(<Index />, document.getElementById('root')); | ||
const container = document.getElementById('root'); | ||
const root = createRoot(container); | ||
root.render( | ||
<React.StrictMode> | ||
<DonkeyComponent /> | ||
<DataFetchComponent /> | ||
</React.StrictMode> | ||
); |