import { useEffect, useState } from "react";
const useFetch = (url) => {
const [data, setData] = useState(null);
const [isPending, setIsPending] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const abortController = new AbortController();
fetch(url, { signal: abortController.signal })
.then((response) => {
if (!response.ok) {
throw Error("Could not fetch the data for that resource");
}
return response.json();
})
.then((data) => {
setData(data);
setIsPending(false);
setError(null);
})
.catch((err) => {
if (err.name === "AbortError") {
console.log("Fetch aborted");
} else {
setIsPending(false);
setError(err.message);
}
});
return () => abortController.abort();
}, [url]);
return { data, isPending, error };
};
export default useFetch;
function MyButton() {
return (
<button>
I'm a button
</button>
);
export default function MyApp() {
return (
<div>
<h1>Welcome to my app</h1>
<MyButton />
</div>
);
//creating react app
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
in cmd
npm init react-app react.js
in cmd
npm run start
import { useState } from "react";
export default function Input() {
const [myinputvalue, setmyinputvalue] = useState("");
function Handdleinput(event) {
setmyinputvalue(event.target.value);
}
return (
<div>
<label> Your name</label>
<input onChange={Handdleinput} type="text" value={myinputvalue}
/>
<h1>{myinputvalue} </h1>
</div>
);
}