Open kunheejeon opened 1 year ago
07/11
http / restful api / axios
fetch(url, {
method: "GET", // POST, PUT, DELETE, etc.
mode: "cors", // no-cors, same-origin
cache: "default", // no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, omit
headers: {
"Content-Type": "application/json", // application/x-www-form-urlencoded
},
redirect: "follow", // manual, error
referrerPolicy: "no-referrer-when-downgrade", // no-referrer, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data), // body data type must match "Content-Type" header
}
fetch('https://api.example.com/data')
.then(response => {
if (!response.ok) { throw new Error('Network response was not ok'); }
return response.json();
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error('Error:', error);
});
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
if (!response.ok) { throw new Error('Network response was not ok'); }
const data = await response.json();
} catch (error) {
console.error('Error:', error);
}
}
fetchData();
useEffect() should not return a promise
useEffect(async() => {})
is not alloweduseEffect(() => { const fetchItem = async () => { await fetch(); } })
firebase databade: https://react-http-f7e24-default-rtdb.firebaseio.com/
React Hooks
setTimeout and promise code questions need to be reviewed.
Redux is a state management system for a cross-component/app-wide state (React 3rd party library)
States: local state: single component => useState()/ useReducer() cross-component state: state that affects multiple components => prop chains / prop drilling app-wide state: state that affect entire app (e.g. user auth status) => useContext() or Redux
React Context vs Redux Context disadvantages: complex setup/management for large code/components
Redux
npm install redux react-redux
src/store/index.js as redux folder+file
in root index.js file,
import { Provider } from "react-redux";
import store from "./store/index";
useSelector()
useDispatch()
dispatch({ type: "testType", actionValue: 123 });
in redux never mutate the existing state, instead override it by returning new state object.
Redux Toolkit createSlice()
configureStore()
const store = configureStore({
reducer: counterSlice.reducer,
});
Single-Page Application Routing
<RouterProvider router={router} />
Generally configure routes make different components are loaded for different paths
To route to other pages
<Link to="/">Home</Link>
to have nav, have root layout
element: <root-page />
then put all other pages into children (nested route)
children: [{
path: 'product',
element: <product />
}]
handling error page
errorElement: <ErrorPages />
active link: if the page is rendering the link, link is being active state css
<NavLink to="/" className={({ isActive }) => isActive ? classes.active : undefined }end></NavLink>
Dynamic Route (use colone : and useParams()) useParams() : params from link
<Link to={
/products/${prod.id}}>{prod.title}</Link>
data fetching and submission
with loader and action
react-route-dom is strong enough for data to be loaded and then loads the page
when loading data, it gives browser a response
so when error occur from loading, throw new Response to handle it (can use json())
useLoaderData()
or useRouteLoaderData('loader-id')
action attribute to submit data
advanced cased: useFetcher()
deferring data fetching
07/10