Skip to content

Commit f5bdddb

Browse files
Merge pull request #1 from michaelboateng1/advanceReact
Advance react
2 parents b0b4433 + 08f8700 commit f5bdddb

29 files changed

+950
-100
lines changed

src/App.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import React from "react";
2+
3+
// CSS
4+
import "./index.css";
5+
6+
// ! /* useState examples */
7+
// import UseStateExample from "./advance_react/useState/useState_basics";
8+
// import UseStateWithArray from "./advance_react/useState/useState_withArray";
9+
// import UseStateWithObj from "./advance_react/useState/useState_objects";
10+
// import Counter from "./advance_react/useState/useState_counter";
11+
12+
// ! /* useEffect examples */
13+
// import UseEffectBasics from "./advance_react/useEffect/useEffect_basics";
14+
// import CleanFunction from "./advance_react/useEffect/useEffect_clean";
15+
// import FetchData from "./advance_react/useEffect/useEffect_fetch-data";
16+
17+
// ! /* conditional rendering */
18+
// import ConditionalRExample from "./advance_react/conditional_rendring/conditional_rendring";
19+
// import GetUser from "./advance_react/conditional_rendring/get-data";
20+
// import ShortCircute from "./advance_react/conditional_rendring/short_circuting";
21+
// import ShortCircuteExample from "./advance_react/conditional_rendring/short_circute-examples";
22+
// import ShowAndHide from "./advance_react/conditional_rendring/show_hide";
23+
24+
// ! /* forms */
25+
// import ControlledInputBasics from "./advance_react/forms/controlled_input";
26+
// import MultipleInputs from "./advance_react/forms/multiple_inputs";
27+
28+
// ! /* useRef */
29+
// import UncontrolledInput from "./advance_react/useRef/uncontrolled_input";
30+
31+
// ! /* useReducer */
32+
// import Index from "./advance_react/useReducer";
33+
34+
// ! /* propDrilling */
35+
// import PropDrilling from "./advance_react/prop_drilling/prop_drilling";
36+
37+
// ! /* ContextAPI/useContext */
38+
// import ContextAPI from "./advance_react/useContext/useContext";
39+
40+
// ! /* Custom Hooks */
41+
import Example from "./advance_react/custom_hooks/customHookExample";
42+
43+
function App() {
44+
return (
45+
<React.Fragment>
46+
<div className="container">
47+
{/* useState hook examples */}
48+
{/* <UseStateExample /> */}
49+
{/* <UseStateWithArray /> */}
50+
{/* <UseStateWithObj /> */}
51+
{/* <Counter /> */}
52+
53+
{/* useEffect hook examples */}
54+
{/* <UseEffectBasics /> */}
55+
{/* <CleanFunction /> */}
56+
{/* <FetchData /> */}
57+
58+
{/* conditional rendering */}
59+
{/* <ConditionalRExample /> */}
60+
{/* <GetUser /> */}
61+
{/* <ShortCircute /> */}
62+
{/* <ShortCircuteExample /> */}
63+
{/* <ShowAndHide /> */}
64+
65+
{/* controlled input */}
66+
{/* <ControlledInputBasics /> */}
67+
{/* <MultipleInputs /> */}
68+
69+
{/* useRef hook */}
70+
{/* <UncontrolledInput /> */}
71+
72+
{/* useReducer hook */}
73+
{/* <Index /> */}
74+
75+
{/* propDrilling */}
76+
{/* <PropDrilling /> */}
77+
78+
{/* contextAPI/useContext */}
79+
{/* <ContextAPI /> */}
80+
81+
{/* Custom Hooks */}
82+
<Example />
83+
</div>
84+
</React.Fragment>
85+
);
86+
}
87+
88+
export default App;

src/Book.js

-37
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React, { useState, useEffect } from "react";
2+
3+
function ConditionalRendering() {
4+
let [isLoading, setIsLoading] = useState(true);
5+
let [user] = useState("Default user");
6+
7+
useEffect(() => {
8+
setTimeout(() => setIsLoading(false), 3000);
9+
}, []);
10+
11+
if (isLoading) {
12+
return <h1>Loading...</h1>;
13+
}
14+
15+
return <h1>{user}</h1>;
16+
}
17+
18+
export default ConditionalRendering;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React, { useEffect, useState } from "react";
2+
3+
const url = "https://api.github.com/users/QuincyLarson";
4+
5+
function GetUser() {
6+
let [isLoading, setIsLoading] = useState(true);
7+
let [isError, setIsError] = useState(false);
8+
let [user, setUser] = useState("Default User");
9+
10+
useEffect(() => {
11+
fetch(url)
12+
.then((resp) => resp.json())
13+
.then((data) => {
14+
if (data.status >= 200 && data.status <= 299) {
15+
console.log(data);
16+
let { login } = data;
17+
setIsLoading(false);
18+
setUser(login);
19+
20+
throw new Error(data.statusText);
21+
} else {
22+
setIsLoading(false);
23+
setIsError(true);
24+
}
25+
})
26+
.catch((error) => console.error(error));
27+
});
28+
29+
if (isLoading) {
30+
return <h1>Loading...</h1>;
31+
}
32+
33+
if (isError) {
34+
return <h1>Error...</h1>;
35+
}
36+
37+
return <h1>{user}</h1>;
38+
}
39+
40+
export default GetUser;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, { useState } from "react";
2+
3+
function ShortCircuteExample() {
4+
const [text] = useState(false);
5+
const [isError, setIsError] = useState(false);
6+
7+
return (
8+
<>
9+
<h1>{text || "Hello World"}</h1>
10+
<h1>{isError && "Error..."}</h1>
11+
<p>{isError ? "There's an error..." : "There's no error"}</p>
12+
<button
13+
type="button"
14+
className="btn"
15+
onClick={() => setIsError(!isError)}
16+
>
17+
Toggle
18+
</button>
19+
</>
20+
);
21+
}
22+
23+
export default ShortCircuteExample;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React, { useState } from "react";
2+
3+
function ShortCircute() {
4+
let [text] = useState("Michael");
5+
let firstText = text || <h1>First circute</h1>;
6+
let secondText = text && <h1>Second circute</h1>;
7+
8+
return (
9+
<>
10+
<div>{firstText}</div>
11+
<div>{secondText}</div>
12+
<h1>{text && "this is a test"}</h1>
13+
</>
14+
);
15+
}
16+
17+
export default ShortCircute;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { useEffect, useState } from "react";
2+
3+
function ShowAndHide() {
4+
const [show, setShow] = useState(false);
5+
6+
return (
7+
<>
8+
{show && <Item />}
9+
<button type="button" className="btn" onClick={() => setShow(!show)}>
10+
Show/Hide
11+
</button>
12+
</>
13+
);
14+
}
15+
16+
function Item() {
17+
const [size, setSize] = useState(window.innerWidth);
18+
19+
const checkSize = () => {
20+
setSize(window.innerWidth);
21+
};
22+
23+
useEffect(() => {
24+
window.addEventListener("resize", checkSize);
25+
return () => {
26+
window.removeEventListener("resize", checkSize);
27+
};
28+
}, []);
29+
return (
30+
<>
31+
<h1>Window</h1>
32+
<h2>{size}</h2>
33+
</>
34+
);
35+
}
36+
37+
export default ShowAndHide;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react";
2+
3+
// importing custom hook
4+
import { useFetch } from "./useFetch";
5+
6+
const url = "https://api.github.com/users";
7+
8+
function Example() {
9+
const { isLoading, data } = useFetch(url);
10+
return <h1>{isLoading ? "Loading..." : "Data is in :)"}</h1>;
11+
}
12+
13+
export default Example;
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useState, useEffect } from "react";
2+
3+
export function useFetch(url) {
4+
const [isLoading, setIsLoading] = useState(true);
5+
const [data, setData] = useState([]);
6+
7+
const getData = async () => {
8+
try {
9+
const response = await fetch(url);
10+
const parseData = await response.json();
11+
setData(parseData);
12+
setIsLoading(false);
13+
} catch (error) {
14+
console.error(error);
15+
}
16+
};
17+
18+
useEffect(() => {
19+
getData();
20+
}, [url]);
21+
22+
return { isLoading, data };
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, { useState } from "react";
2+
3+
function ControlledInputBasics() {
4+
const [name, setName] = useState("");
5+
const [email, setEmail] = useState("");
6+
const [people, setPeople] = useState([]);
7+
8+
const hundleSubmit = (e) => {
9+
e.preventDefault();
10+
if (name && email) {
11+
setPeople((people) => {
12+
return [...people, { id: new Date().getTime().toString(), name, email }];
13+
});
14+
setName("");
15+
setEmail("");
16+
} else {
17+
console.log("Please fill in your name and email");
18+
}
19+
};
20+
21+
return (
22+
<>
23+
<article>
24+
<form onSubmit={hundleSubmit}>
25+
<div className="form-controlle">
26+
<label htmlFor="name">Name: </label>
27+
<input type="text" id="name" name="fullName" value={name} onChange={(e) => setName(e.target.value)} />
28+
</div>
29+
<div className="form-controlle">
30+
<label htmlFor="email">Email: </label>
31+
<input type="text" id="email" name="email" value={email} onChange={(e) => setEmail(e.target.value)} />
32+
</div>
33+
<button type="submit">Add Person</button>
34+
</form>
35+
</article>
36+
{people.map((person) => {
37+
const { id, name, email } = person;
38+
return (
39+
<div className="person" key={id}>
40+
<h4>{name}</h4>
41+
<p>{email}</p>
42+
</div>
43+
);
44+
})}
45+
</>
46+
);
47+
}
48+
49+
export default ControlledInputBasics;

0 commit comments

Comments
 (0)