*If the user wants to click anywhere of the box to move to the other page, use event bubbling.
: Executing from child component to parent component. (์์์์ ๋ถ๋ชจ๋ก ์ด๋ฒคํธ ์ ํ)
Meaning, even if onclick function is bined to parent, outside onclick is also executed when the clicking child.
event.currentTarget.id
Party of event bubbling. (๋น์ฌ์)
--> eventTarget is the thing the user directly clicked.
--> currentTarget is declaring the function directly.
event.capturing
Exectued from parent to child.
Scope: {}
Scope-Chain: Happens when the key cannot find its value inside its scope when doing "console.log". By scope-chaining, the key can find it's value in the outside scope.
const [count, setCount] = useState(0)
~
setCount(count + 1)
When the user types 0 in the intial value in useState ==> no error.
But when the user doesn't type anything in the initial value in useState, error occurs.
REASON:
Typescript deduces the type. If there isn't any types delclared, typescript deduces as "undefined" type.
--> Since it deduced as undefined, the typescript is looking for undefined but since (count + 1) is number type, it makes error.
Typescript deduces based on the initial value, so there should be 0 to tell that the state will be in number form.
To make that code upside to be working, the user must use prev method.
Refactoring a function
setCount((prev) => prev + 1)
setCount((prev) => prev + 1)
setCount((prev) => prev + 1)
setCount((prev) => prev + 1)
setCount((prev) => prev + 1)
Prev is original count value. If there is any saved values inside count, it is accumulating the saved values inside the pre-saving area.
To be specific,
<span onClick={onClickPage} id="1"> 1 </span>
<span onClick={onClickPage} id="2"> 2 </span>
<span onClick={onClickPage} id="3"> 3 </span>
โฌ๏ธ Originally, we can make like this separately, but the code below is much more simple. โฌ๏ธ
{new Array(10).fill(1).map((_, index) => (
<span key={index + startPage} onClick={onClickPage} id={String(index + startPage)}>
{` ${index + startPage} `}
</span>
))}
โก๏ธ Here, the developer is using new Array() to make a new array that will be put into the page. Also, using state and setState, the user can go back and forth of pagination.
const lastPage = Math.ceil(dataBoardsCount?.fetchBoardsCount / 10)
Also to delete the paginations that don't contain any data should be deleted; so there we used Math.ceil() to round up the number of data and get the needed number of pagination.
What if one wants to show child component 1 's state in child component 2?
--> Since they aren't in a relationship of parent and child, the state must be spread through parent component by lifting state up. Then, it can be used by using props in child components.
Used with library. (react-infinite-scroller)
hasMore = (true)
loadMore