๐Ÿ˜ฎTIL 0118

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

CodeCamp FE 05

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

[Review]

โœ”๏ธ Re-rendering

  • Redrawing the page when the state changes.
    --> The displayed screen seems like everything are linked to each other, but it's actually re-drawing the whole code for multiple times.

โœ”๏ธ Button Click Event

 if(event.target.value && myPassword && myTitle && myContents){
        setIsActive(true)
      } else {
        setIsActive(false)
      }
  • If the condition is true, the button's color changes into yellow.
  • We're using event.target.value to make sure of all datas getting pre-saved into pre-save library.
  • The data is currently inside the pre-saving zone, which will eventually be executed when all data are in.

โœ”๏ธ Button / Cursor

background-color: ${(props) => props.isActive === true ? "yellow" : "none"};
  
  :hover {
    cursor: ${(props) => props.isActive === true ? "pointer" : "default"};
  }                     // ๋งŒ์•ฝ false์ผ๋–„ ํ•˜๋ ค๋ฉด !props.isActive ? pointer ---
`;
  • When {isActive} becomes true statement, the color changes into yellow.
  • When all the information is put, the cursor changes into pointer so that the user can click the button and go on to the next stage.

โœ”๏ธ Router Error

  • If there are board-new page and board-detail page, same component could be used in both components.
  • router.query.aaa will be activated in detail page, but errors occur in board-new page.
  • The results can change by the pages.

โœ”๏ธ Simple-Codes
/boards : View board lists (๊ฒŒ์‹œ๋ฌผ ๋ชฉ๋ก๋ณด๊ธฐ)
/boards/new : Post the board (๊ฒŒ์‹œ๋ฌผ ๋“ฑ๋ก)
/boards/:boardId : Detail pages (์ƒ์„ธ๋ณด๊ธฐ)

/boards/edit --> This doesn't work. There should be the board's own number, which is ID.
So it should go as: boards/:boardId/edit

/products : View product lists
/products/new : New product posted

  • When naming the variables or functions, consider other users too.

[Map]

๐Ÿ’ญ Map is more efficient than for loop.

const classmates = ["Debbie","Dereck","Dmitri"]
classmates.map ((el) => ("Student" + el))
  • el: element
  • Approaching to elements inside the classmates array, and "student"+el eventually results:
    --> Student Debbie // Student Dereck // Student Dmitri

Same for objects

classmates.map((el)=>({name: ${name: `Student ${el.name}`}))

--> Making a new object and re-putting the object inside. (Triangle Form)

๐Ÿ–‡ Connecting to HTML

const FRUITS = [
    { number: 1, title: "๋ ˆ๋“œํ–ฅ" },
    { number: 2, title: "์ƒค์ธ๋จธ์Šค์ผ“" },
    { number: 3, title: "์‚ฐ์ฒญ๋”ธ๊ธฐ" },
    { number: 4, title: "ํ•œ๋ผ๋ด‰" },
    { number: 5, title: "์‚ฌ๊ณผ" },
    { number: 6, title: "์• ํ”Œ๋ง๊ณ " },
    { number: 7, title: "๋”ธ๊ธฐ" },
    { number: 8, title: "์ฒœํ˜œํ–ฅ" },
    { number: 9, title: "๊ณผ์ผ์„ ๋ฌผ์„ธํŠธ" },
    { number: 10, title: "๊ทค" },
];

export default function MapFruitsPage(){
    const ccc = FRUITS.map((el)=>(<div>{el.number} {el.title}</div>))
    return(
        <div>{ccc}</div>
    )
}
  • Or instead of putting {ccc}, directly writing const ccc code itself can be more efficient.

โžฐ Removing unnecessary brackets:

classmates.map((el)=> "Student"+el)
classmates.map((el)=>(name: `student + $(el.name)`)) 
  //--> wrong

const add 5 = ()=> {
  return {name: `student ${el.name}`}
  //--> Correct
  • The user cannot delete the small bracket that encompasses {name}.

[Filter]

  • When 3 values are being filted, there can't be more than 3 returned.
  • The user may use filter precedingly then use map. The order doesn't really matter.

[Boards Checkbox / Delete]

import {gql, useQuery, useMutation} from '@apollo/client'
import styled from '@emotion/styled'

const FETCH_BOARDS = gql`
    query fetchBoards{
        fetchBoards{
            number
            writer
            title
            createdAt
        }
    }
`

const DELETE_BOARD = gql`
    mutation deleteBoard($number: Int){
        deleteBoard(number: $number){
            message
        }
    }
`

const Row = styled.div`
    display: flex;
`
const Column = styled.div`
    width: 20%;
`

export default function MapCheckboxDeletePage (){
    const [deleteBoard] = useMutation(DELETE_BOARD)
    const {data} = useQuery(FETCH_BOARDS)

    const onClickDelete = (event) => {
        deleteBoard({
            variables: {number: Number(event.target.id)},
            refetchQueries:[{query: FETCH_BOARDS}]
        })
    }

    return(
        <div>
            {data?.fetchBoards?.map((el)=>(
                <Row key={el.number}>
                    <Column><input type="checkbox"/></Column>
                    <Column>{el.number}</Column>
                    <Column>{el.title}</Column>
                    <Column>{el.writer}</Column>
                    <Column>{el.createdAt}</Column>
                    <Column><button id={el.number} onClick={onClickDelete}>์‚ญ์ œ</button></Column>
                                                        {/* event handler */}
                </Row>
            ))}
        </div>
    )
}

๐Ÿ“• event.target.id
: Gets the written ID and request for deleting that row.

๐Ÿ“” Before using evemt.target.id,

  • The data is deleted in database excel zone, but not in user's screen. To see that the row is deleted, the use had to refresh the browser.(refetch)

[Key / Index]

๐Ÿ”‘ When the user checked the checkbox and deleted that row but the checkbox isn't getting deleted, use key.

  • Key saves all data separately.
    --> To save the data in separate way, all data should contain it's own value.
    --> number is the only own value up there in the code since the number is created when the boards are created.
  • Index cannot be insulted in key.
    --> Deleting is possible but the key still exists. --> Error occurs

[Algorithms - .reduce() and .slice() ]

๐Ÿง€ slice

  • Able to use in strings and arrays
  • Can copy the array (slightly)

arr = [1,2,3,4];
arr.slice(1,3)
โ€”>2๋ถ€ํ„ฐ ์ž๋ฅด๊ฒ ๋‹ค

๐Ÿฅฌ reduce

  • Calculates all the data and returns them
  • Only to use in arrays
arr = [2,3,4,5];
arr.reduce((cu, el) => {
//cu: current - piled data (๋ˆ„์ ๊ฐ’)
	console.log(cu, el);
	return cu+el
},0)
--> starts to pile up from this number = 0
sum;
profile
๋‘๋น„๋‘๋ฐฅ๋ฐฅ

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