DanKim0213 / Today-I-Learned

Today I Learned
1 stars 0 forks source link

Reactjs #16

Open DanKim0213 opened 1 year ago

DanKim0213 commented 1 year ago

Whatever relates to React!

DanKim0213 commented 9 months ago

How we declare states

SQL Normalization helps us to declare states

According to principles for structuring state:

there are a few principles that can guide you to make better choices.

DanKim0213 commented 9 months ago

Why React JS is a Library not Framework

The main difference between a framework and a library lies in a term called ”inversion of control”.

React is referred to as “unopinionated” because there are lots of options while Next.js is a framework of React JS which provides a lot of built-in features like routing, server-side rendering, image optimization, and much more.

DanKim0213 commented 9 months ago

Optimizing Performance

There are several ways you can speed up your React application.

(Note that Don’t optimize prematurely!)

How

Avoid unnecessary Effects that update state. Most performance problems in React apps are caused by chains of updates originating from Effects that cause your components to render over and over.

If a specific interaction still feels laggy, use the React Developer Tools profiler to see which components would benefit the most from memoization, and add memoization where needed. In the long term, we’re researching doing granular memoization automatically to solve this once and for all.

References:

DanKim0213 commented 9 months ago

React Server Components

React Server Components (or RSC) is a new application architecture designed by the React team.

Server Components can run during the build, letting you read from the filesystem or fetch static content. They can also run on the server, letting you access your data layer without having to build an API. You can pass data by props from Server Components to the interactive Client Components in the browser.

RSC combines the simple “request/response” mental model of server-centric Multi-Page Apps with the seamless interactivity of client-centric Single-Page Apps, giving you the best of both worlds.

React Server Components has shipped in Next.js App Router.

References

DanKim0213 commented 9 months ago

React.dev

What changes are there on the site?

React.dev team refers "browser rendering" as "painting" to avoid confusion throughout the docs.

The previous React documentation site has now moved to legacy.reactjs.org.

References

DanKim0213 commented 9 months ago

What I learned while reading Memo

https://react.dev/reference/react/memo

  1. Memo itself
  2. You might not need Memo
  3. Accepting components as JSX Children do not cause re-rendering => let’s test using console.log
  4. useMemo and useCallback are used together with Memo

“React normally re-renders a component whenever its parent re-renders. With memo, you can create a component that React will not re-render when its parent re-renders so long as its new props are the same as the old props. “

https://react.dev/reference/react/memo#should-you-add-memo-everywhere “If your app is like this site, and most interactions are coarse (like replacing a page or an entire section), memoization is usually unnecessary. On the other hand, if your app is more like a drawing editor, and most interactions are granular (like moving shapes), then you might find memoization very helpful.”

“When a component visually wraps other components, let it accept JSX as children. This way, when the wrapper component updates its own state, React knows that its children don’t need to re-render.”

https://react.dev/reference/react/memo#minimizing-props-changes “When you use memo, your component re-renders whenever any prop is not shallowly equal to what it was previously. This means that React compares every prop in your component with its previous value using the Object.is comparison. Note that Object.is(3, 3) is true, but Object.is({}, {}) is false.” “ if the prop is an object, prevent the parent component from re-creating that object every time by using useMemo:” “When you need to pass a function to memoized component, either declare it outside your component so that it never changes, or useCallback to cache its definition between re-renders.”

DanKim0213 commented 9 months ago

Custom Hooks let you share stateful logic, not state itself

https://react.dev/learn/reusing-logic-with-custom-hooks

whenever you write an Effect, consider whether it would be clearer to also wrap it in a custom Hook. Wrapping it into a custom Hook lets you precisely communicate your intent and how the data flows through it.

With time, the React team’s goal is to reduce the number of the Effects in your app to the minimum by providing more specific solutions to more specific problems. When React adds new features, you can remove those Effects without changing any of your components.

reason for why wrapping Effects in custom Hooks is often beneficial:

  1. You make the data flow to and from your Effects very explicit.
  2. You let your components focus on the intent rather than on the exact implementation of your Effects.
  3. When React adds new features, you can remove those Effects without changing any of your components.

The code inside your custom Hooks will re-run during every re-render of your component. This is why, like components, custom Hooks need to be pure. Think of custom Hooks’ code as part of your component’s body!

DanKim0213 commented 9 months ago

New APIs that seems useful

use() for fetching data useSyncExternalStore() for sync

DanKim0213 commented 9 months ago

Q. is it appropriate to pass down setState() by itself to child components, rather than a concrete event handler?

No because React keeps its design pattern focused on concrete high-level use cases.

For example, custom hook and Hook api itself.

To comply with its pattern, we should pass a concrete handler down to child components.

DanKim0213 commented 8 months ago

React Batches

Automatic Batching

Starting in React 18 with createRoot, all updates will be automatically batched, no matter where they originate from. This means that updates inside of timeouts, promises, native event handlers or any other event will batch the same way as updates inside of React events

React batches state updates

React waits until all code in the event handlers has run before processing your state updates. This is why the re-render only happens after all these setNumber() calls."

DanKim0213 commented 8 months ago

Data Suspense

Q. isLoading vs isLoaded

React Batches are automatically happens. Thus,

const [isLoading, setLoading] = useState(false);
useEffect(() => {
  setLoading(false);
  fetch('/items').then(res => res.json()).then(data => { setItems(data); setLoading(false); });
}, []); // setLoading(true) and setLoading(false); batch together. Therefore, not allowed.

Instead,

const [isLoaded, setLoaded] = useState(false);
useEffect(() => {
  fetch('/items').then(res => res.json()).then(data => { setItems(data); setLoaded(true); });
}, []); 

Of course, flushSync can helps you to prevent the batch. But, in most cases, it decreases the React performance.

Q. React.Suspense