๐Ÿง‹TIL 0114

JBยท2022๋…„ 1์›” 14์ผ
0

CodeCamp FE 05

๋ชฉ๋ก ๋ณด๊ธฐ
5/33

[Routing]

: Moving from one page to the other by using router

Code:

const router = useRouter()
router.push("web page address")

--> Then the user can go in to the web page address.

Types of router:

router.reload() ex) this command can refresh the page
router.replace()
router.pathname()

etc.

  • Router is a type of object: If the user types < router. > (period after router), then vs code shows other functions that is related after router. .

[Static Routing / Dynamic Routing]

โฌ‡๏ธ Static Routing

import {useRouter} from 'next/router'

export default function StaticRoutingPage(){
    const router = useRouter()
    
    const onClickMove1= (event)=> {
        router.push("/05-04-static-routed-board/1")
    }
    
    const onClickMove2= (event)=> {
        router.push("/05-04-static-routed-board/2")
    }

    const onClickMove3= (event)=> {
        router.push("/05-04-static-routed-board/3")
    }

    return (
        <div>
            <button onClick={onClickMove1}>1๋ฒˆ ๊ฒŒ์‹œ๊ธ€๋กœ ์ด๋™ํ•˜๊ธฐ</button>
            <button onClick={onClickMove2}>2๋ฒˆ ๊ฒŒ์‹œ๊ธ€๋กœ ์ด๋™ํ•˜๊ธฐ</button>
            <button onClick={onClickMove3}>3๋ฒˆ ๊ฒŒ์‹œ๊ธ€๋กœ ์ด๋™ํ•˜๊ธฐ</button>
        </div>
        )
}

โฌ‡๏ธ Static Routed

export default function StaticRoutedPage(){

    return(
        <div>1๋ฒˆ ๊ฒŒ์‹œ๊ธ€ ํŽ˜์ด์ง€ ์ด๋™์™„๋ฃŒ</div>
    )
}
-----------------------------------------------
export default function StaticRoutedPage(){

    return(
        <div>2๋ฒˆ ๊ฒŒ์‹œ๊ธ€ ํŽ˜์ด์ง€ ์ด๋™์™„๋ฃŒ</div>
    )
}
-----------------------------------------------
export default function StaticRoutedPage(){

    return(
        <div>3๋ฒˆ ๊ฒŒ์‹œ๊ธ€ ํŽ˜์ด์ง€ ์ด๋™์™„๋ฃŒ</div>
    )
}
  • Static Routing needs import: import {useRouter] from 'next/router'
  • Static Routing needs exact amount of static routed pages to execute. If the user wants to make 3 buttons and wants to enable all 3 of them to be able to move to the other page, the user needs to make a separate 1,2,3 folders and files.

โฌ‡๏ธ Dynamic Routing

import {useRouter} from 'next/router'

export default function DynamicRoutingPage(){
    const router = useRouter()
    
    const onClickMove1= (event)=> {
        router.push("/05-06-dynamic-routed-board/1")
    }
    
    const onClickMove2= (event)=> {
        router.push("/05-06-dynamic-routed-board/2")
    }

    const onClickMove3= (event)=> {
        router.push("/05-06-dynamic-routed-board/3")
    }

    const onClickMove4= (event)=> {
        router.push("/05-06-dynamic-routed-board/4")
    }

    const onClickMove100= (event)=> {
        router.push("/05-06-dynamic-routed-board/100")
    }

    return (
        <div>
            <button onClick={onClickMove1}>1๋ฒˆ ๊ฒŒ์‹œ๊ธ€๋กœ ์ด๋™ํ•˜๊ธฐ</button>
            <button onClick={onClickMove2}>2๋ฒˆ ๊ฒŒ์‹œ๊ธ€๋กœ ์ด๋™ํ•˜๊ธฐ</button>
            <button onClick={onClickMove3}>3๋ฒˆ ๊ฒŒ์‹œ๊ธ€๋กœ ์ด๋™ํ•˜๊ธฐ</button>
            <button onClick={onClickMove4}>4๋ฒˆ ๊ฒŒ์‹œ๊ธ€๋กœ ์ด๋™ํ•˜๊ธฐ</button>
            <button onClick={onClickMove100}>100๋ฒˆ ๊ฒŒ์‹œ๊ธ€๋กœ ์ด๋™ํ•˜๊ธฐ</button>

        </div>
        )
}

โฌ‡๏ธ Dynamic Routed

import {useRouter} from 'next/router'
import {useQuery, gql} from '@apollo/client'

const FETCH_BOARD = gql`
    query feetchBoard($number: Int){
        fetchBoard(number : $number){
            writer
            title
            contents
        }
    }
`

export default function DynamicRoutedPage(){
    const router = useRouter()

    const {data} = useQuery(FETCH_BOARD, {
        variables: {number: Number(router.query.qqq)}
    })

    console.log(data)

    return (
        <div>
            <div>{router.query.qqq}๋ฒˆ ๊ฒŒ์‹œ๊ธ€ ํŽ˜์ด์ง€ ์ด๋™ ์™„๋ฃŒ</div>
            <div>์ž‘์„ฑ์ž:{data?.fetchBoard?.writer}</div>
            <div>์ œ๋ชฉ: {data?.fetchBoard?.title}</div>
            <div>๋‚ด์šฉ: {data?.fetchBoard?.contents}</div>
        </div>
    )
}
  • For mutation, it needed [any names of variables] that the user wanted to execute. However, for query, it is limited to {data}. It cannot be changed into any other names.
  • Alike mutation, query also needs variable within the bracket after the comma. (-- useQuery(FETCH_BOARD, {variables: {---}}))
  • To check whther the data is well-operating, the user must check with < console.log(data) >.
  • Dynamic routing only requires one folder [aaa](can be in any kind of name) and it's more efficient than static routing.

Difference:

  • Static routing is activated when user types ~~boards/100. Specific page number is required.
  • Dynamic routing is activated just by [aaa].
    --> Can just enter in related index, which automatically saves into [aaa].
    --> It pulls out the specific variable data and let users see it.
  • [aaa]: variable name(Could be anything, like "myname")
    --> It is no more page, but a variable name.
    --> When user type boards/1, it is automatically saves into aaa, so it goes boards/aaa.
    --> Thus, only one index.js is okay in [aaa] folder. One folder, multi pages can be moved through qqq and distributes data.
    --> If executed, ~~board/3

[aaa] Code:

(First Post boards: http://localhost:3000/boards/1(or [aaa]))

const router = userRouter()
router.query={aaa: 1} //part that is executed

How it works:

const router = useRouter()
const {data} = useQuery(FETCH_BOARD)

  • These commands go to backend with requests. --> Backend saves them to data base and responses with data
    *That data is put into useQuery(FETCH_DATA) and it sets the data into {data}.

[Conditional Rendering]

(Ko. ์กฐ๊ฑด๋ถ€ ๋ Œ๋”๋ง)

  • When the code ends as {data.fetchBoard.contents}, the computer says that the data is undefined. So the code must be in asynchronous state, which mutiple data can be saved or be in a process at the same time.
  • So {data && data.fetchBoard?.writer} is used to make sure that the data is shown when the data exists. If not, then none of data is shown.
  • "It's literally like conditional statement. If the condition matches, computer recognizes the code as "true" so the code is executed successfully. But if the computer recognizes as "false", that false-code isn't executed and the following codes are executed.

[Optional Chaining]

  • Since the developers wanted to make conditional rendering even more condnese, the made optional changing.
  • Optional changing is literally combining redundant code inside conditional rendering.
  • The code goes like this: {data?.fetchBoard?.writer}

[Template Literal ]

const apppleNum = 3
    const bananaNum = 10
    console.log("์ฒ ์ˆ˜๋Š” ์‚ฌ๊ณผ๋ฅผ "+ apple +"๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๋ฐ”๋‚˜๋‚˜๋ฅผ " + banana) + "๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์š”"
    console.log(`JB has ${appleNum} apples and ${bananaNum} bananas`)
  • Template Literal enables the user to write execution code in a simple way with ${} and backticks.
  • Users used to make execution codes as: ("JB has "+appleNum+"apples and "+ bananaNum + "bananas.")
    --> Here, template literal is deleting all the "quotation marks" and plus signs. All the codes go inside backticks and use $dollar sign and {curly brackets}.

[try () / catch(error)]

  • try() helps users to check the errors before actually running the file.
  • catch(error) finds and tells the errors from backend. When there happens an error, rest of the codes are immediately skipped and it takes the user to finally{} to execute the final code anyway.

[Shorthand Property]

  • If key and value take same variables, then the user can shorten the code:
    writer: writer; --> writer;

[Algorithms]

  • return () function executes the code once it matches the condition. It does not look for the rest of the code when once it is executed.
  • Conditional statements are executed from top to bottom like looking at the condition one by one. So computer takes look at all the codes and find out the execution code that highly matches with the condition.
profile
๋‘๋น„๋‘๋ฐฅ๋ฐฅ

0๊ฐœ์˜ ๋Œ“๊ธ€