๐Ÿ˜ซ TIL 0117

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

CodeCamp FE 05

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

[Review]

  • Basically when the user requests backend computer for submiting the posts, there appears a way called API for two computers to communicate.
  • Then the backend computer puts the data inside the data base and figures out whether the data is successfully saved or not.
  • Considering the result, backend computer responses to frontend computer.
  • If (data) is true, then the data is printed. But if the data is false, the data can not be saved nor displayed.
  • Global state is a state that can be used anywhere in the pages.
    --> const {data} is considered to be global states.

[Optional-chaining vs. Nullish-coalescing]

๐Ÿ’ก Nullish-coaliescing: data ?? data.fecthProfile

  • If data is false, the data is activated.

  • It's the opposite concept from optional chaining .

  • data || "Hello"
    --> If the data is literally false, the code is executed

  • data ?? "Hello"
    --> If the data is empty, null, or undefined, the code is executed.

False Types:

0 --> All real numbers except zero are true.
'' --> Things that are empty are also considered "false"

  • This is different from " ", which contains "space".
    false
    null --> Empty
    undefined --> Empty
  • ex) If the user declared a as 3 but then he wants to delete it, he uses a=undefined. This is used especially when the user is working with other co-workers. Usually people use "undefined" rather than null.
    NaN --> Not a Number

Example Codes

true && "Hello"
-->'์ฒ ์ˆ˜์ž…๋‹ˆ๋‹ค.'
"ใ…‡" && "Hello"
-->'์ฒ ์ˆ˜์ž…๋‹ˆ๋‹ค.'
//The data is true since it contains string

10 && "Hello"
--> '์ฒ ์ˆ˜์ž…๋‹ˆ๋‹ค.'
//This works since the number isn't zero

false || "World"
-->'์˜ํฌ์ž…๋‹ˆ๋‹ค.'
"" || "World"
-->'์˜ํฌ์ž…๋‹ˆ๋‹ค.'
12 || "World"
-->12
//Since the data is true and it's Nullish coaleascing, it doesn't work. It works only when the data is false. 
0 || "World"
-->'์˜ํฌ์ž…๋‹ˆ๋‹ค.'
20 || "World"
-->20

null ?? "Hi"
'ํ›ˆ์ด์ž…๋‹ˆ๋‹ค.'
undefined ?? "Hi"
'ํ›ˆ์ด์ž…๋‹ˆ๋‹ค.'
0 ?? "Hi"
0
--> Hi๋Š” null์ด๋‚˜ undefined ์ผ๋•Œ๋งŒ ์‹คํ–‰๋จ

[Folder Structure]

๐Ÿ’ก Container/Presenter
Presenter is the part where users can see the displayed screen.
Javascript zone becomes container and JSX zone becomes presenter.

  • For the naming, the period matters only at the end of the file's name since it decides which kind of file it will turn out to be.
  • When the user executes the source code, presenter goes into container's return statement. So eventaully the two files are connected via importing and exporting.
import BoardWriteUI from "./BoardWrite.presenter"

โžก๏ธ So here, the user is importing the presenter to container.

If the variables aren't used, there happends an error even though two files are connected successfully.
--> This means that the functions and variables inside presenter doesn't work in contianer. Therefore, the user must declare in container, which is the parent component.

๐Ÿ”” Here comes the idea of parent component and child component. The container is like the head of the whole function, which means it's the most important part.
--> So to connect every single data of these compoents, props are needed. Also, there is no way for the parent components to directly inherit variables and functions to their child components.



Props

: A function that the parent functions is inheriting the variables and functions that they contain to their child components.

๐Ÿ›  How it works

: After connecting thier child components in return statement, the variables and functions are sub-declared. An object is created, and the parent function goes into the object as a form of value. Here, the key is "props".
ex) props = {aaa: onChangeWriter, bbb: {writer}}
--> So the function is now available to use.

React
: The flow of data is in one way, one direction.
From top to bottom, parent component to child component.

Angular
: The flow of data is bidirectional.
But this makes hard for users to catch the flow.

Emotion component
Emotion is also a component, so it is possible to get through components.
For emotion, presenter becomes their parent component and there are bunch of emotion child components.

[Commons]


[Practice Codes]

โฌ‡๏ธ Container-Presenter

import BoardWrite from '../../src/components/units/board/write/BoardWrite.container'
export default function GraphqlMutationArgsInput(){

    return (
        <BoardWrite/>
    )

}

โฌ‡๏ธ Container.js

import axios from 'axios'
import {useState} from 'react'
import { useMutation} from '@apollo/client'
import BoardWriteUI from './BoardWrite.presenter'
import {CREATE_BOARD} from './BoardWrite.queries'

export default function BoardWrite(){
        const [isActive, setIsActive]=useState(false)

        const [myWriter, setMyWriter] = useState("")
        const [myTitle, setMyTitle] = useState("")
        const [myContents, setMyContnets] = useState("")

    
        const [aaa, setAaa] = useState("")
        const [qqq] = useMutation(CREATE_BOARD)
    
        const zzz = async () => {
            
        	const result = await qqq({ 
            	variables: { writer: myWriter, title:myTitle, contents: myContents } 
            })
            console.log(result.data.createBoard.message)
            setAaa(result.data.createBoard.message)
        }
    
        const onChangeMyWriter = (event) => {
            setMyWriter(event.target.value)
            if (myWriter && myTitle && myContents){
                setIsActive(true)
            }
        }
        const onChangeMyTitle = (event) => {
            setMyTitle(event.target.value)
            if (myWriter && myTitle && myContents){
                setIsActive(true)
            }
        }
        const onChangeMyContents = (event) => {
            setMyContnets(event.target.value)
            if (myWriter && myTitle && myContents){
                setIsActive(true)
            }
        }

        return (
           <BoardWriteUI
           bbb={aaa}
           ccc={zzz}
           eee={onChangeMyWriter}
           fff={onChangeMyTitle}
           ddd={onChangeMyContents}
           isActive={isActive}
           />
        )
       
    }

โฌ‡๏ธ Presenter.js

import * as S from './BoardWrite.styles'

export default function BoardWriteUI(props){

    return (
        <>                                 
            ์ž‘์„ฑ์ž: <S.MyInput type="text" onChange={props.ddd} /><br />
            ์ œ๋ชฉ: <S.MyInput type="text" /><br />
            ๋‚ด์šฉ: <S.MyInput type="text" /><br />
            <S.MyButton onClick={props.ccc} ggg={props.isActive}>GRAPHQL-API ์š”์ฒญํ•˜๊ธฐ</S.MyButton>
            <div>{props.bbb}</div>
        </>
    )
}

โฌ‡๏ธ Queries.js

import {gql} from '@apollo/client'

export const CREATE_BOARD = gql `
mutation createBoard($writer: String, $title: String, $contents: String) {
    createBoard(writer: $writer, title: $title, contents: $contents){
        _id
        number
        message
    }
}
`

โฌ‡๏ธ Styles.js

import styled from '@emotion/styled'

export const MyInput = styled.input``

export const MyButton = styled.button`
    background-color: ${(props) => props.ggg === true ? "yellow" : "none"};
`
profile
๋‘๋น„๋‘๋ฐฅ๋ฐฅ

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