Skip to content

Commit

Permalink
useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Oct 25, 2022
1 parent 162aae7 commit 8061365
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
15 changes: 9 additions & 6 deletions react-hooks/home/src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { useState } from 'react';
import Joke from './Joke';

function App() {
const [userQuery, setUserQuery] = useState('');

const searchQuery = () => {
window.open(`https://google.com/search?q=${userQuery}`, '_blank');
const updateUserQuery = event => {
console.log('userQuery', userQuery);

setUserQuery(event.target.value);
}

const handleKeyPress = event => {
Expand All @@ -13,10 +16,8 @@ function App() {
}
}

const updateUserQuery = event => {
console.log('userQuery', userQuery);

setUserQuery(event.target.value);
const searchQuery = () => {
window.open(`https://google.com/search?q=${userQuery}`, '_blank');
}

return (
Expand All @@ -30,6 +31,8 @@ function App() {
/>
<button onClick={searchQuery}>Search</button>
</div>
<hr />
<Joke />
</div>
);
}
Expand Down
27 changes: 27 additions & 0 deletions react-hooks/home/src/Joke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { useEffect, useState } from 'react';

function Joke() {
const [joke, setJoke] = useState({});

useEffect(() => {
fetch('https://official-joke-api.appspot.com/jokes/random')
.then(response => response.json())
.then(json => {
console.log('joke json', json);

setJoke(json);
});
}, []);

const { setup, punchline } = joke;

return (
<div>
<h3>Joke of the session</h3>
<p>{setup}</p>
<p><em>{punchline}</em></p>
</div>
);
}

export default Joke;

0 comments on commit 8061365

Please sign in to comment.