|
1 | | -import { Header } from "./components/Header"; |
2 | | -import { ThoughtForm } from "./components/ThoughtForm"; |
3 | | -import { ThoughtList } from "./components/ThoughtList"; // Removed duplicate declaration of App |
4 | | -import { useState } from "react"; |
5 | | -import { useEffect } from "react"; |
6 | | -import "./components/App.css"; |
7 | | -import "./components/Card.css"; |
8 | | -import "./components/index.css"; |
9 | | - |
10 | | -import "./App.css"; |
11 | | - |
12 | | -// Removed duplicate declaration of App |
13 | | - |
14 | | -export const App = () => { |
15 | | - const [count, setCount] = useState(0); |
16 | | - |
17 | | - useEffect(() => { |
18 | | - const handleScroll = () => { |
19 | | - console.log("scrolled!"); |
| 1 | +import React, { useEffect, useState } from "react"; |
| 2 | +import "./index.css"; |
| 3 | + |
| 4 | +function Header() { |
| 5 | + useEffect(() => { |
| 6 | + console.log('mount') |
| 7 | + }, []) |
| 8 | + return ( |
| 9 | + <header className="App-header"> |
| 10 | + <h1>Message App Happy Thoughts</h1> |
| 11 | + </header> |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 15 | +export default function App() { |
| 16 | + const [text, setText] = useState(""); |
| 17 | + const [error, setError] = useState(""); |
| 18 | + const handleChange = (e) => { |
| 19 | + if (e.target.value.length > 140) { |
| 20 | + setError("character limit exceeded"); |
| 21 | + } else { |
| 22 | + setText(e.target.value); |
| 23 | + setError(null); |
| 24 | + } |
20 | 25 | }; |
21 | 26 |
|
22 | | - window.addEventListener("scroll", handleScroll); |
23 | | - return () => { |
24 | | - window.removeEventListener("scroll", handleScroll); |
25 | | - }; |
26 | | - }, []); |
27 | | - |
28 | | - useEffect(() => { |
29 | | - const controller = new AbortController(); |
30 | | - |
31 | | - fetch("https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts", { |
32 | | - signal: controller.signal, |
33 | | - }) |
34 | | - .then((response) => response.json()) |
35 | | - .then((data) => console.log(data)); |
36 | | - |
37 | | - const intervalId = setInterval(() => { |
38 | | - fetch("https://happy-thoughts-ux7hkzgmwa-uc.a.run.app/thoughts", { |
39 | | - signal: controller.signal, |
40 | | - }) |
41 | | - .then((response) => response.json()) |
42 | | - .then((data) => console.log(data)); |
43 | | - |
44 | | - console.log("This runs every second"); |
45 | | - }, 1000); |
46 | | - |
47 | | - return () => { |
48 | | - clearInterval(intervalId); |
49 | | - controller.abort(); |
50 | | - }; |
51 | | - }, []); |
52 | | - |
53 | | - return ( |
54 | | - <> |
55 | | - <Header /> |
56 | | - <main> |
57 | | - <ThoughtForm /> |
58 | | - <ThoughtList /> |
59 | | - <div> |
60 | | - <button onClick={() => setCount(count + 1)}>Increase count</button> |
61 | | - <button onClick={() => setCount(count - 1)}>Decrease count</button> |
62 | | - <button onClick={() => setCount(0)} disabled={count === 0}> |
63 | | - Reset |
64 | | - </button> |
65 | | - <button onClick={() => setCount(count * 2)}>Multiply</button> |
66 | | - |
67 | | - <p>Count: {count}</p> |
68 | | - {count > 140 && <p>You hit 140!</p>} |
| 27 | + return ( |
| 28 | + <div className="App"> |
| 29 | + <Header /> |
| 30 | + <div className="input-container"> |
| 31 | + <input |
| 32 | + className="input" |
| 33 | + type="text" |
| 34 | + placeholder=" enter something ..." |
| 35 | + onChange={handleChange} |
| 36 | + value={text} |
| 37 | + /> |
| 38 | + <span className="error">{error && error}</span> |
| 39 | + </div> |
69 | 40 | </div> |
70 | | - </main> |
71 | | - </> |
72 | | - ); |
73 | | -}; |
| 41 | + ); |
| 42 | +} |
0 commit comments