๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿฟ Project

[Side PJ] ์•ผ๊ตฌ๊ฒŒ์ž„ - ์‚ฝ์งˆ๊ณผ ๋ฐฐ์šด์ ๐Ÿ› 

by Tamii 2021. 5. 20.
๋ฐ˜์‘ํ˜•

 

๋ณต์žกํ•œ ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํ•œ ์•ผ๊ตฌ๊ฒŒ์ž„์„ ๋งŒ๋“ค์–ด ๋ณด๋Š” ๋ฏธ๋‹ˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค.

๋ฐฑ์—”๋“œ์—์„œ API๋ฅผ ์ œ๊ณตํ•ด ์ฃผ์—ˆ๊ณ ,  ๊ฐ„๋‹จํ•œ ๋กœ์ง๊ฐ™์€ ๊ฒฝ์šฐ๋Š” FE๋‹จ์—์„œ ๊ด€๋ฆฌํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ๊ฒช์—ˆ๋˜ ํฌ๊ณ  ์ž‘์€ error๋“ค๊ณผ ๋ฐฐ์› ๋˜ ์ ์„ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค-!

 

 

 


 

 

 

VS Code ์‚ฌ์†Œํ•œ ๋ฌธ๋ฒ• ๋งž์ถ”๊ธฐ

 [ Setting -> quote ๊ฒ€์ƒ‰ -> ์ˆ˜์ • ]

 

์˜คํ†ฐ๊ณผ ํ•จ๊ป˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•˜๋ฉด์„œ ๋ฐœ๊ฒฌํ•œ ์‚ฌ์†Œํ•œ ๋ฒˆ๊ฑฐ๋กœ์›€์€ 

๋ฐ”๋กœ VS Code ์ €์žฅ ์‹œ ์ž๋™ ์ ์šฉ๋˜๋Š” ๋ฌธ๋ฒ• ์„ค์ •์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ

 

JS์—์„œ ๋ฌธ์ž์—ด์ด auto๋กœ ๋˜์–ด์žˆ๊ณ 

Prettier ์ƒ JSX ๋„ ์ฒดํฌ๊ฐ€ ์•ˆ๋˜์žˆ์—ˆ์ง€๋งŒ 

 

single, check ๋กœ ๋งž์ถ”์—ˆ๋‹ค.

 

mergeํ• ๋•Œ ์ด๋Ÿฐ ์‚ฌ์†Œํ•œ ๋ถ€๋ถ„์ด ๋ณ€๊ฒฝ์‚ฌํ•ญ์œผ๋กœ ๊ธฐ๋ก๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ณ ์ณ์ฃผ์—ˆ๋Š”๋ฐ ํ˜‘์—…ํ•  ๋•Œ ์ด๋Ÿฐ ์‚ฌ์†Œํ•œ ๋ฌธ์ œ๋„ ๋ฐœ์ƒํ•˜๋Š”๊ตฌ๋‚˜ ํ•˜๋Š” ํ—คํ”„๋‹~

 

 

 

 

 

 

 

 

  + Router ์„ค์ • 

 

์˜ค๋Š˜์˜ ์‚ฝ์งˆ: module์„ ์„ค์น˜ํ•˜๋ฉด ( styled-components ๋ฅผ ์„ค์น˜ํ–ˆ๋‹ค) ์„œ๋ฒ„๋ฅผ ํ•œ๋ฒˆ ๊ป๋‹ค ์ผœ์•ผํ•˜๋Š”๋ฐ 

๋งˆ์Œ์ด ๊ธ‰ํ•˜๋‹ค๋ณด๋‹ˆ ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋‹ค.


๐Ÿ“Œ ThemeProvider ๋กœ ๊ณตํ†ต Theme ๋งŒ๋“ค์–ด ๊ด€๋ฆฌํ•˜๊ธฐ

 <ThemeProvider>

 : Context API๋ฅผ ํ†ตํ•ด ์ž์ฒด ์•„๋ž˜์˜ ๋ชจ๋“  React ๊ตฌ์„ฑ์š”์†Œ์— ๊ณตํ†ต ํ…Œ๋งˆ๋ฅผ ์ œ๊ณตํ•˜๋Š” styled-components ๋ž˜ํผ๊ตฌ์„ฑ ์š”์†Œ

 

App์—์„œ <ThemeProvider>๋กœ ๊ฐ์‹ผ ์ž์‹ Component๋“ค์€ props๋กœ theme์„ ์ „๋‹ฌ๋ฐ›์•„

๊ฐ๊ฐ์˜ Component์—์„œ ๋”ฐ๋กœ ์„ ์–ธ ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

์‚ฌ์šฉ๋ฒ•

1) sytled-components ์—์„œ ThemeProvider  import 

2) ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•  Component์š”์†Œ๋ฅผ ๊ฐ์‹ผ๋‹ค

 

App.jsx

import { BrowserRouter, Route, Switch } from "react-router-dom";
import { createGlobalStyle, ThemeProvider } from "styled-components";
import "./App.css";
import PlayScreen from "./routes/PlayScreen";
import StartScreen from "./routes/StartScreen";
import theme from "./theme";

function App() {
  const GlobalStyle = createGlobalStyle`
  * {
    padding:0;
    margin:0;
  }
  
  body{
    box-sizing:border-box;
  }
  
  ol,ul {
    list-style: none;
  }
  `;

  return (
    <ThemeProvider theme={theme}>
      <GlobalStyle />
      <BrowserRouter>
        <Switch>
          <Route path="/" exact>
            <StartScreen />
          </Route>
          <Route path="/play-screen">
            <PlayScreen />
          </Route>
        </Switch>
      </BrowserRouter>
    </ThemeProvider>
  );
}

export default App;

 

 

theme.js

const theme = {
  fontSizes: {
    XS: "14px",
    S: "16px",
    M: "18px",
    L: "20px",
    XL: "24px",
    XXL: "40px",
  },

  screenType: {
    START: "start",
    PLAY: "play",
  },
};

export default theme;

 

์ด๋ ‡๊ฒŒ styled-components์—์„œ  ์ œ๊ณตํ•˜๋Š” ThemeProvider๋ฅผ ์ƒ๋‹จ ๋ถ€๋ถ„์— ๊ฐ์‹ธ์ฃผ๋ฉด 

theme์— ์žˆ๋Š” ์†์„ฑ๋“ค์„ ๋”ฐ๋กœ import ํ•  ํ•„์š” ์—†์ด globalํ•˜๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

Component์—์„œ์˜ ์‚ฌ์šฉ

const TeamCurrent = styled.div`
  font-size: ${({ theme }) => theme.fontSizes.XS};
  color: ${({ theme }) => theme.colors.orange};
`;

 

โš ๏ธ  ๋‹จ ์†์„ฑ์œผ๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒฝ์šฐ๋กœ๋Š” ์‚ฌ์šฉ์ด ์•ˆ๋œ๋‹ค ใ… ใ… 

import Title from "../components/Title";

const PlayScreen = (props) => (
  <>
    <h1>playํ™”๋ฉด</h1>
    <Title type={theme.screenType.PLAY} />
  </>
);

export default PlayScreen;

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•ˆ๋จน์Œ 

 

๋Š๋‚€์  

 

 

์žฅ์  ๋‹จ์ 
- ํ”„๋กœ์ ํŠธ ์ „๋ฐ˜ ์Šคํƒ€์ผ์— ์ผ๊ด€์„ฑ ์ƒ๊น€
- ํŒ€ ๋‚˜๋ฆ„์˜ StyleConvention์ด ์ƒ๊น€
- ์ด๊ฒƒ ๋˜ํ•œ ํŒ€์˜ convention์ด๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๋ฐ˜์— ๊ทœ์น™ ์„ค์ •์— ์‹œ๊ฐ„์ด ์†Œ์š”๋จ

 


๐Ÿ“Œ CRA์˜ public path๋Š” public

์ฒ˜์Œ ์ด๋ฏธ์ง€๊ฒฝ๋กœ๋ฅด ์„ค์ •ํ•  ๋•Œ  src ํด๋”์— ๋„ฃ์–ด๋‘๊ณ  ์ƒ๋Œ€๊ฒฝ๋กœ๋กœ ์—ฐ๊ฒฐํ–ˆ๋”๋‹ˆ img๊ฐ€ ๋ถˆ๋Ÿฌ์™€์ง€์ง€ ์•Š์•˜๋‹ค.

 

๊ฒ€์ƒ‰์„ ํ•ด๋ณด๋‹ˆ CRA ์—์„œ image๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒฝ์šฐ

1) ์ ˆ๋Œ€ ๊ฒฝ๋กœ ์‚ฌ์šฉ

2) public / images ํด๋”๋ฅผ ์ƒ์„ฑํ•ด ์‚ฌ์šฉ

์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค.

https://stackoverflow.com/questions/37644265/correct-path-for-img-on-react-js

 

Correct path for img on React.js

I have some problem with my images on my react project. Indeed I always thought that relative path into src attribute was built on the files architecture Here my files architecture: components

stackoverflow.com

 

image ์‚ฌ์šฉ component

import styled from 'styled-components';

const Stadium = () => {
  return (
    <StadiumDiv></StadiumDiv>
  );
};

const StadiumDiv = styled.div`
  box-sizing: border-box;
  opacity: 90%;
  background-image: url('./baseballRound.jpg');
  background-position: center;
`;


export default Stadium;

public/baseballRound.jpg๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ–ˆ๋‹ค.


๐Ÿ“Œ ContextAPI ์ ์šฉํ•˜๊ธฐ

ConstextAPI๋ฅผ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์• ์ผ๋‹ค.

์ผ๋‹จ ๋‚˜๋Š” ์—ฌํƒœ๊ป ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ  ์œ„ํ•ด props drilling or useState ๋งŒ ์‚ฌ์šฉํ•ด์™”์—ˆ๋Š”๋ฐ 

์ข€๋” ์ƒ์œ„์—์„œ props drilling์„ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๊ณ  ์ƒํƒœ๊ด€๋ฆฌ๋ฅผ ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ„์ธ ContdxtAPI์—๋Œ€ํ•ด ๊ณต๋ถ€ํ–ˆ๊ณ  ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ๊ฒฐ์‹ฌ!

 

์‚ฌ์šฉ๋ฒ•

 

1) provider ์ƒ์„ฑ : ์ „์—ญ์‚ฌ์šฉํ•  data ์ƒ์„ฑ  Context.js

2) reducer ์ƒ์„ฑ : useReducer ์ด์šฉํ•œ  state, dispatch ์ƒ์„ฑ  ( Provider์žˆ๋Š”๊ณณ ํ˜น์€ ๋ถ„๋ฆฌํ•ด๋„ ๋จ)

3) App ์—์„œ (ํ˜น์€ ์ƒ์œ„์—์„œ ) Provider ๋กœ ๋ฌถ์–ด์ฃผ๊ธฐ - ์ด๋•Œ reducer ํ•จ์ˆ˜ ์ „๋‹ฌ 

4)  ์‚ฌ์šฉํ•  ๊ณณ์—์„œ importํ›„ ์‚ฌ์šฉ  ({state, dispatch} = useContext(newContext) )

 

 

provider/Context.jsx

import { createContext } from 'react';

const boardHistory = {
  S: 0,
  B: 0,
  O: 0,
  H: 0,
  HitInfo: ' ',
};

const BoardHistoryContext = createContext();

export { boardHistory, BoardHistoryContext };

์ „์—ญ์‚ฌ์šฉํ•  state ์ƒ์„ฑ ( boardHistory, BoardHistoryContext)

 

 

reducer/ ballReducer.js

const ballReducer = (ballCnt, action) => {
  switch (action.type) {
    case 'hitS':
      return { ...ballCnt, S: ballCnt.S + 1 };

    case 'hitB':
      return { ...ballCnt, B: ballCnt.B + 1 };
      
    default:
      return null;
  }
};

export default ballReducer;

ballReducer ๋ผ๋Š” dispatch์— ๋”ฐ๋ผ state๊ด€๋ฆฌํ•  reducer ํ•จ์ˆ˜ ์ƒ์„ฑ

 

App.js

import { boardHistory, BoardHistoryContext } from './components/provider/Context';
import ballReducer from './components/reducer/ballReducer';

function App() {
  const [ballCnt, dispatch] = useReducer(ballReducer, boardHistory);


  return (
    <ThemeProvider theme={theme}>
      <GlobalStyle />
      <BrowserRouter>
        <Switch>
          <AppDiv>
            <Route path='/' exact>
              <StartScreen />
            </Route>
            <BoardHistoryContext.Provider value={{ ballCnt, dispatch }}>
              <Route path='/play-screen'>
                <PlayScreen />
              </Route>
            </BoardHistoryContext.Provider>
          </AppDiv>
        </Switch>
      </BrowserRouter>
    </ThemeProvider>
  );
}

ํ•ด๋‹น ์ „์—ญ state๋ฅผ ์‚ฌ์šฉํ•  Component  Provider๋กœ  ๊ฐ์‹ธ๊ธฐ

 

 

 

 

Stadium.jsx( ์‚ฌ์šฉํ•˜๋Š” ์ž์‹)

import { boardHistory, BoardHistoryContext } from '../provider/ContextB';

const Stadium = () => {
  const { ballCnt, dispatch } = useContext(BoardHistoryContext);
  
    return (
    <StadiumDiv>
    
    //...์ค‘๋žต
    
      <PlayButton onClick={() => playPitch(dispatch, ballCnt)}>
        PITCH
      </PlayButton>
    </StadiumDiv>
  );
};


const playPitch = (callback, ballCnt) => {

  const tempBoardLst = ['S', 'B', 'H'];
  
  var randomBoard =
    tempBoardLst[Math.floor(Math.random() * tempBoardLst.length)];
  callback({ type: randomBoard });
  
  dispatch({ type: 'hitInfo', payload: randomHit });
  dispatch({ type: 'hit' + randomHit });
};

useContext ์— ์ •์˜ ํ›„

PlayBoard component , playPitch ํ•จ์ˆ˜ ์ด์šฉํ•˜์—ฌ 

PlayButton์— ํด๋ฆญ ์ด๋ฒคํŠธ ์ „๋‹ฌ 

<PlayButton onClick={() => playPitch(dispatch, ballCnt)}>

 

PlayButton์„ ๋ˆ„๋ฅด๋ฉด ๋žœ๋ค๊ฐ’์— ๋”ฐ๋ผ ballreducer์˜ dispatch ๊ฐ€ ์ ์šฉ๋˜๋„๋ก ํ–ˆ๋‹ค.

 

 


๐Ÿ“Œ useEffect๋กœ ์ƒํƒœ ๊ด€๋ฆฌํ•˜๊ธฐ

๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๋ฃจํ‹ด์€ 

1)  ๋ฒ„ํŠผ ํด๋ฆญ

2) 3๊ฐ€์ง€ ๊ฒฝ์šฐ ์ค‘ ํ•˜๋‚˜ ์„ ํƒ -> ํ•ด๋‹น ๊ฐ’ +1 (Context ๋กœ ๊ด€๋ฆฌ)

3) Component๋Š” ํ•ด๋‹น๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ์žฌ๋ Œ๋”๋ง

 

return ๋ถ€๋ถ„

<PlayBoardTemp type='S' />
<PlayBoardTemp type='B' />
<PlayBoardTemp type='O' />

type์— ๋”ฐ๋ผ ๋ Œ๋”๋ง

 

 

playPitch (๋ฒ„ํŠผ ํด๋ฆญ์‹œ ์‹คํ–‰๋  ํ•จ์ˆ˜)

const playPitch = (ballCnt, dispatch) => {
  const tempBoardLst = ['S', 'B'];
  const randomHit =
    tempBoardLst[Math.floor(Math.random() * tempBoardLst.length)];
  console.log('1');
  dispatch({ type: 'plus' + randomHit });

  if (ballCnt.S > 3) {
    console.log('์•„์›ƒ์ž…๋‹ˆ๋‹ค');
    setTimeout(() => dispatch({ type: 'plusO' }), 1000);
  }
  if (ballCnt.B > 4) {
    console.log('์•ˆํƒ€์ž…๋‹ˆ๋‹ค');
    setTimeout(() => dispatch({ type: 'plusH' }), 1000);
  }
};

 

๋‚˜์˜ ์˜ˆ์ƒ์œผ๋กœ๋Š”

ํด๋ฆญ ->  dispatch({type: 'plus' _ randomHit}); ์‹คํ–‰

->  ํ•ด๋‹น์กฐ๊ฑด์— ๋งž์œผ๋ฉด if ๋ฌธ์ด ์‹คํ–‰ ์ด์—ˆ์ง€๋งŒ

 

์‹ค์ œ๋กœ๋Š” 

ํด๋ฆญ ->  dispatch ์ „ if ๋ฌธ ์‹คํ–‰

-> dispatch({type: 'plus' _ randomHit}); ์‹คํ–‰ 

์ด ์ˆœ์„œ๋กœ ์ง„ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ฑด์ด ๋‹ค์Œ ํด๋ฆญ์‹œ์— ์ ์šฉ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

 

 

 

 

์ˆ˜์ • ์ „ PlayPitch

const playPitch = (ballCnt, dispatch) => {
  const tempBoardLst = ['S', 'B'];
  const randomHit =
    tempBoardLst[Math.floor(Math.random() * tempBoardLst.length)];

  if (ballCnt.S < 2 && ballCnt.B < 3) {
    dispatch({ type: 'plus' + randomHit });
  }
  if (ballCnt.S === 2) {
    dispatch({ type: 'plus' + randomHit });

    setTimeout(() => dispatch({ type: 'plusO' }), 1000);
  }
  if (ballCnt.B === 3) {
    dispatch({ type: 'plus' + randomHit });

    setTimeout(() => dispatch({ type: 'plusH' }), 1000);
  }
};

์ˆ˜์ • ํ›„ PlayPitch

const playPitch = (ballCnt, dispatch) => {
  const tempBoardLst = ['S', 'B', 'H'];
  const randomHit =
    tempBoardLst[Math.floor(Math.random() * tempBoardLst.length)];
  console.log(randomHit);
  dispatch({ type: 'hitInfo', payload: randomHit });
  dispatch({ type: 'hit' + randomHit });
};
 useEffect(() => {
    if (ballCnt.S === 3) {
      setTimeout(() => dispatch({ type: 'hitO', payload: 'O' }), 1000);
    }
    if (ballCnt.B === 4) {
      setTimeout(() => dispatch({ type: 'hitH', payload: 'H' }), 1000);
    }
    if (ballCnt.O === 3) {
      setTimeout(() => dispatch({ type: 'resetAll' }), 1000);
    }
  }, [ballCnt]);

 

์ˆ˜์ •์ „ PlayPitch๋ฅผ ๋ณด๋ฉด 

ballCnt ๋ฅผ ์–ต์ง€๋กœ ์กฐ์ ˆํ•˜๊ณ  ์žˆ๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

๋‚ด๊ฐ€ ์›ํ–ˆ๋˜ ์ƒํ™ฉ์€ ์ด๊ฑธ ์˜ˆ์ƒํ–‡์ง€๋งŒ  

1) ๋ฒ„ํŠผ ํด๋ฆญ

2) dispatch ๋™์ž‘ [ ballCnt +=1 ์ฆ๊ฐ€ ]

3) ์กฐ๊ฑด๋ฌธ ๋™์ž‘ [ ์ฆ๊ฐ€ํ•œ์ƒํƒœ์˜ ๊ฐ’์—์„œ S==3์ด๋ฉด ์•„์›ƒ B===4์ด๋ฉด ์•„์›ƒ์ธ๋ฐ ]

 

(์ˆ˜์ •์ „์˜ PlayPitch์—์„œ) ๋™์ž‘

1) ๋ฒ„ํŠผ ํด๋ฆญ

2) ์กฐ๊ฑด๋ฌธ ๋™์ž‘ [ prev dispatch ๋กœ ๋™์ž‘ ] 

3) dispatch ๋™์ž‘ [ ballCnt ++ ]

 

์‹ค์ œ๋กœ๋Š” ์ด๋ ‡๊ฒŒ ๋™์ž‘ํ–ˆ๋‹ค. 

์™œ๋ƒ๋ฉด ๋‚˜๋Š” ์™„์ „ํžˆ ์ž˜๋ชป ์ƒ๊ฐํ–ˆ๋˜ ๊ฒƒ์ด , 

๋ฒ„ํŠผ์ดํด๋ฆญ๋˜๊ณ  PlayPitch๊ฐ€ ์‹คํ–‰๋˜๋ฉด dispatch๊ฐ€ ๋™์ž‘๋˜๋ฉด์„œ ์ „์ฒด Component ์žฌ ๋ Œ๋”๋ง์ด ๋œ๋‹ค  

๋˜ ํด๋ฆญ๋  ๋‹น์‹œ์˜ ballCnt์˜ ์ƒํƒœ๋Š” ๋‚ด๊ฐ€ ์›ํ•œ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์ด๊ธฐ ๋•Œ๋ฌธ์— 

๋‹ค์Œ ํด๋ฆญ์„ ๋ˆ„๋ฅด๋ฉด์„œ ๋ณ€ํ™”ํ•œ ballCnt ์ƒํƒœ๋กœ ์กฐ๊ฑด๋ฌธ์ด ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋ž˜์„œ ์ฒ˜์Œ์—๋Š” ๊ตฌ์„ฑ์ด ์ž˜๋ชป๋œ ๊ฑธ ํŒŒ์•…ํ•˜์ง€ ๋ชปํ•œ ์ฑ„ 

์–ต์ง€๋กœ ์ƒํƒœ๋ฅผ ์šฐ๊ฒจ๋„ฃ์—ˆ๋‹ค.

 

 

useEffect๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ballCnt๊ฐ€ ๋ณ€๊ฒฝ์ด ๋˜๋ฉด ๋ฐ”๋กœ ๋™์ž‘ํ•˜๋„๋ก ์ƒ์„ฑํ–ˆ๋‹ค.

 

1) PlayPictch onClick ์‹คํ–‰ 

2) random ํ•˜๊ฒŒ dispatch ์‹คํ–‰

3)  ballCnt๋ณ€๊ฒฝ

4) ๋ณ€๊ฒฝ ์ดํ›„์˜ ์ƒํƒœ๋กœ if ์กฐ๊ฑด ์ ์šฉ

 

์ด 4๋‹จ๊ณ„๋ฅผ ํ†ตํ•ด ํˆฌ์ˆ˜๊ฐ€ ๊ณต์„ ๋˜์กŒ์„ ๋•Œ ์ผ์–ด๋‚˜๋Š” ์ผ์„ ๊ด€๋ฆฌํ–ˆ๋‹ค.

 

 

๋Š๋‚€์  

 

 

react์—์„œ ์ƒํƒœ๊ด€๋ฆฌ์˜ ์ค‘์š”์„ฑ์„ ๋‹ค์‹œ๊ธˆ ๊นจ๋‹ฌ์•˜๋‹ค.

 ํŠนํžˆ ์ƒํƒœ์— ๋”ฐ๋ฅธ ์กฐ๊ฑด๋ฌธ๊ณผ ๊ทธ ๊ณผ์ •์„ ์ฒ˜๋ฆฌํ•  ๋•Œ 

์ฝ”๋“œ๊ฐ€ ๋‚ด ์ƒ๊ฐ๋Œ€๋กœ ๋™์ž‘ํ• ๊ฑฐ๋ผ ์ƒ๊ฐํ–ˆ์ง€๋งŒ 

๋ฆฌ์•กํŠธ๋Š” ์ƒํƒœ๊ฐ€ ๊ฐ์ง€ํ•จ๊ณผ ๋™์‹œ์ œ ์žฌ ๋ Œ๋”๋ง ๋˜๊ธฐ ๋•Œ๋ฌธ์— 

๋ฐ‘์˜ ์กฐ๊ฑด๋“ค์€ ๊ทธ ๋‹ค์Œ์ƒํƒœ๊ฐ€ ๋˜์–ด์„œ์•ผ ์ ์šฉ์ด ๋œ๋‹ค๋Š” ์‚ฌ์‹ค์„ ๊นจ๋‹ฌ์•˜๋‹ค. 

 

์ƒํƒœ๊ด€๋ฆฌ์˜ ๋Šช์— ๋น ์ ธ์žˆ์„ ๋•Œ DD์˜ ๋„์›€์„ ๋งŽ์ด ๋ฐ›์•˜๊ณ  .

DD๊ฐ€ ์จ์ฃผ์‹  ๋ธ”๋กœ๊ทธ์— ์ƒํƒœ๊ด€๋ฆฌ์˜ ๊ธฐ๋ณธ์ ์ธ ์›๋ฆฌ๊ฐ€ ์ž˜ ์„ค๋ช…๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๐Ÿ”—๋งํฌ๋ฅผ ๋‚จ๊น๋‹ˆ๋‹ค.

 

 

https://velog.io/@jjunyjjuny/React-useState%EB%8A%94-%EC%96%B4%EB%96%BB%EA%B2%8C-%EB%8F%99%EC%9E%91%ED%95%A0%EA%B9%8C

 

[ React ] useState๋Š” ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ• ๊นŒ

useState์˜ ๋™์ž‘ ์›๋ฆฌ์— ๋Œ€ํ•ด์„œ

velog.io

 

๋Œ“๊ธ€