๐Ÿ“Day 78(52)์ผ์ฐจ WE_MOTIVATION_DIARY

18729 ๋‹จ์–ด MOTIVATION_DIARYMOTIVATION_DIARY

- ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ํ˜„ํ™ฉ

  • ๋‚ด๊ฐ€ ๋งŒ๋“  ํŽ˜์ด์ง€ : ๋ฆฌ์ŠคํŠธ ํŽ˜์ด์ง€
  • ๊ธฐ๋Šฅ ๊ตฌํ˜„ :
  1. ์ œํ’ˆ ํ˜ธ๋ฒ„์‹œ ์ƒํ’ˆ ๋””ํ…Œ์ผ ์—ฐ์† 4๋ฒˆ ๋ณ€๊ฒฝ,
  2. ์ƒ‰์ƒ๋ณ„ ์ œํ’ˆ ์‚ฌ์ง„ ๋ณ€๊ฒฝ
  3. ๋ฌดํ•œ ์Šคํฌ๋กค ๋ฐ ๋กœ๋”ฉ ํ‘œํ˜„
  4. ๋ฐ˜์‘ํ˜• ์ƒํ’ˆ ๊ฐฏ์ˆ˜๋ฅผ 5๊ฐœ์—์„œ 2๊ฐœ๋กœ
  5. ํ•˜๋‹จ์— gototopbutton๊ณผ top์œผ๋กœ ๊ฐ€๋Šฅ ๊ธฐ๋Šฅ ๊ตฌํ˜„
  6. gototopbutton์— ๋ฒ„ํŠผ์— ํ•˜์ด๋ผ์ดํŠธ ๊ธฐ๋Šฅ

- ํ”„๋ก ํŠธ ํ”„๋กœ์ ํŠธ ์ตœ์ข…ํ˜„ํ™ฉ

  • ํƒœ๊ทœ๋‹˜์˜ ๋ฉ”์ธํŽ˜์ด์ง€, ๋‚˜์˜ ๋ฆฌ์ŠคํŠธ ํŽ˜์ด์ง€, ๋™๊ถŒ๋‹˜์˜ ๋””ํ…Œ์ผ ํŽ˜์ด์ง€
  • ์ฐฝํ˜„๋‹˜์˜ ๋กœ๊ทธ์ธ, ํšŒ์›๊ฐ€์ž… ํœ˜๋ฏผ๋‹˜์˜ nav,footer

- TODAY I LEARN ERROR ๐Ÿฆ 

  • fetch๋ฅผ 10๊ฐœ์”ฉ ๋Š์–ด์„œ ๋ชจ๋‘ ๋ฟŒ๋ ธ์„ ๋•Œ ๋งˆ์ง€๋ง‰ ๋กœ๋”ฉ ์ด๋ฏธ์ง€๊ฐ€ ์‚ฌ๋ฆฌ์ง€์ง€ ์•Š๋Š” ์˜ค๋ฅ˜์— ๋Œ€ํ•ด์„œ
<script>
class ProductList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      listData: [],
      totalCountDataFetched: 10,
      loading: false,
      noData: false,
    };
  }
 // ๋ฐ์ดํ„ฐ๋ฅผ 10๊ฐœ์”ฉ ํŽ˜์น˜๋ฐ›๋Š” ํ•จ์ˆ˜
  fetchMoreData = async () => {
    const LIST_API = '/data/ProductList/PRODUCT_LIST_DATA.json';
    const { totalCountDataFetched, noData } = this.state;
    fetch(LIST_API)
      .then(res => {
        return res.json();
      })
      .then(data => {
        const duplicatedData = [...data.LIST_DATA.product];
        const newDatalistData = duplicatedData.slice(0, totalCountDataFetched);
        this.setState({
          listData: newDatalistData,
        });
        if (this.state.listData.length === 50) {
          this.setState({
            noData: !noData,
          });
          return window.removeEventListener('scroll', this.handleScroll);
        }
      })
      .catch(console.error);
  };
</script>

- HOW TO FIX MY ERROR ๐Ÿ’Š

  • ์ž„์‹œ๋ฐฉํŽธ์œผ๋กœ ๋ฐ์ดํ„ฐ์˜ ์ด๊ฐฏ์ˆ˜, 50๊ฐœ๊ฐ€ ๋˜์—ˆ์„ ๋•Œ state๊ฐ’ noData์„ true๋กœ๋งŒ๋“ฌ์œผ๋กœ์„œ ๋งˆ์ง€๋ง‰ ์Šคํฌ๋กค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ์กฐ์น˜๋ฅผ ํ•ด๋†“์•˜์Œ.
  • ๋ฉ˜ํ† ๋‹˜๊ป˜ ์—ฌ์ญค๋ณด๋‹ˆ ์‹ค์ œ๋กœ๋Š” ๋ฒก์—”๋“œ์—์„œ ํ”„๋ก ํŠธ๊ฐ€ ์š”์ฒญํ•œ ๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์ค„ ๊ฒƒ์ด๊ณ  ์ด๋ฅผ ๋ฒก์—”๋“œ์˜ ๊ณผ์ •์—†์ด ์ง„ํ–‰ํ•˜๊ฒŒ๋œ๋‹ค๋ฉด json ๋ชฉ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ๋ณด๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹Œ 10๊ฐœ์”ฉ ์ž˜๋ผ์„œ ๋ณด๋‚ด๋Š” ๊ฒƒ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜์‹ฌ
  • ERROR๋ฅผ ํ•ด๊ฒฐํ•œ setstate์˜ ์ธ์ž prep๐Ÿ”‘
  1. this.setState(prev => ({...prev, listData: newDatalistData, currentState: prev.currentState + 1, totalState: data.totalCount,})); ๋กœ ์ด์ „ state๊ฐ’๋“ค์„ prep... ์Šคํ”„๋ ˆ๋“œ๋กœ ๋ฐ›์•„์„œ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  2. currentState, ์ „์ฒด ํŽ˜์ด์ง€๋„ค์ด์…˜ ์ˆ˜, ์ฆ‰ ๋‚˜์˜ ๊ฒฝ์šฐ ๋กœ๋”ฉ์„ ๋ช‡๋ฒˆํ•  ๊ฑด์ง€์™€ totalState, ์ „์ฒด ์ƒํ’ˆ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐฑ์—”๋“œ๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ ๋’ค fetch(LIST_API + this.state.currentState + '.json')์ฒ˜๋Ÿผ json ๋ชฉ๋ฐ์ดํ„ฐ๋ฅผ 5๊ฐœ๋กœ ๋‚˜๋ˆ ์„œ fetch ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
    3.this.state.currentState === data.totalCount ๋ฒก์—”๋“œ์—์„œ ๋ฐ›์•„์˜จ ์ˆ˜๊ฐ€ ์ผ์น˜ํ–ˆ์„ ๋•Œ ์Šคํฌ๋กค ์ด๋ฒคํŠธ๋ฅผ ๋ฉˆ์ถฐ์ค˜์•ผํ•˜๊ณ  componentWillUnmount ๋ฉ”์„œ๋“œ๋กœ ์ด๋ฒคํŠธ ์ทจ์†Œ๊นŒ์ง€ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
  • ์ˆ˜์ •๋œ ์ฝ”๋“œ
<script>
class ProductList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      listData: [],
      totalCountDataFetched: 10,
      loading: false,
      currentState: 1,
      totalState: 1,
    };
  }
  componentDidMount() {
    this.fetchMoreData();
    return window.addEventListener('scroll', this.handleScroll);
  }
  componentWillUnmount() {
    // ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์—์„œ ์‚ญ์ œ
    window.removeEventListener('scroll', this.handleScroll);
  }
  fetchMoreData = async () => {
    const LIST_API = '/data/ProductList/PRODUCT_LIST_DATA_'
    const { totalCountDataFetched } = this.state;
    fetch(LIST_API + this.state.currentState + '.json')
      .then(res => {
        return res.json();
      })
      .then(data => {
        const duplicatedData = [...data.LIST_DATA.product];
        const newDatalistData = duplicatedData.slice(0, totalCountDataFetched);
        if (this.state.currentState === data.totalCount) {
          // ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ ์‚ญ์ œ
        }
        this.setState(prev => ({
          ...prev,
          listData: newDatalistData,
          currentState: prev.currentState + 1,
          totalState: data.totalCount,
        }));
      })
      .catch(console.error);
  };
</script>

- ๋ชจ๋ฅด๋Š” ์ 

  • ๋งŒ๋“  ์ž‘์—…๋ฌผ๋“ค์„ ํ•˜๋‚˜๋กœ mergeํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด์„œ
  • comnponentdidupdata๐Ÿ”‘ , componentWillUnmount๐Ÿ”‘ ๋ฌดํ•œ ์Šคํฌ๋กค์ด ํŽ˜์ด์ง€ ๋์—์„œ๋Š” ์‚ฌ๋ผ์ง€๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์— ์Šคํฌ๋กค์„ ๋ฉˆ์ถ”๋Š” ์กฐ๊ฑด์„ ๊ฑธ์–ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ

- ๋ฐฐ์šด์ 

  • ํŒ€์›๋“ค๊ณผ ํ† ์š”์ผ๋ถ€ํ„ฐ~๋ชฉ์š”์ผ๊นŒ์ง€ ์ž‘์—…ํ•œ ํ”„๋ก ํŠธ์—”๋“œ ๊ณผ์ •์„ mergeํ•˜๋Š” ๊ณผ์ •์„ ๊ฒฝํ—˜ํ•˜์˜€์Œ
  • mergeํ•˜๋Š” ๊ณผ์ •์—์„œ conflict๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ์ถฉ๋Œ์„ ํ•ด๊ฒฐํ•˜๊ณ  ๋‹ค์‹œ mergeํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ๋ชจ๋“  ํŽ˜์ด์ง€๋ฅผ main ๋ธŒ๋žœ์น˜์— ํ•ฉ์น  ์ˆ˜ ์žˆ์—ˆ์Œ
    1. merge ํ•˜๊ธฐ ์ „ ๋ฆฌ๋ทฐ 2๊ฐœ ์Šน์ธ ๋ฐ›๊ณ  git merge๋ฒ„ํŠผ ๋ˆ„๋ฅด๊ธฐ
  1. ํŒ€์› ํ•œ ๋ช…์ด ๋ณธ์ธ์˜ ๋ธŒ๋žœ์น˜๋ฅผ mergeํ–ˆ์œผ๋ฉด ๋‹ค๋ฅธ ํŒ€์›์ด ์ž์‹ ์˜ ์ž‘์—… ๋ธŒ๋žœ์น˜,feature์—์„œ mergeํ•œ main ๋ธŒ๋žœ์น˜ git pull ๋ฐ›๊ณ  conflict ํ•ด๊ฒฐํ•˜๊ธฐ
  2. conflict ํ•ด๊ฒฐ๋˜๋ฉด ๋‹ค์‹œ git add์™€ commit ๋‚จ๊ธฐ๊ณ  pushํ•˜๊ธฐ
  3. ์ด ๊ณผ์ •์„ ๋ชจ๋‘ ๋งˆ์ณ ๋งˆ์ง€๋ง‰ ํŒ€์›์ด ์ตœ์ข…์ ์œผ๋กœ mergeํ•˜์—ฌ ์ž‘์—…๋ฌผ ํ•ฉ์น˜๊ธฐ

- ์ž˜ํ•œ ์ ๊ณผ ๊ฐœ์„ ํ•  ์ 

  • ์ง‘์—์˜ค์ž๋งˆ์ž ์ผ์ฃผ์ผ๊ฐ„์˜ ํ”ผ๋กœ๋ฅผ ์œ„ํ•ด ๋ฐ”๋กœ ์ž” ์ ๊ณผ ์ผ์ฐ ์ผ์–ด๋‚œ ์ ์€ ์ž˜ํ•œ ๊ฒƒ๊ฐ™๋‹ค~
  • ํŒ€์›๋“ค๊ณผ ํ•จ๊ป˜ ํ•˜๋ฉด์„œ ํ”„๋ก ํŠธ์—”๋“œ ๋ถ€๋ถ„์˜ ๋ฆฌํŒฉํ† ๋ง๊ณผ ์ˆ˜์ •์„ ๋” ํ•  ์ˆ˜ ์žˆ๋Š” ์‹œ๊ฐ„์ด ์žˆ์Œ์—๋„ ์—ด์‹ฌํžˆ ์ง‘์ค‘ํ•˜์ง€ ์•Š์€ ๊ฒƒ ๋ฐ˜์„ฑํ•˜์ž!

- ๋ชฉํ‘œ์™€ ๋งˆ๊ฐ๊ธฐํ•œ: 2021-10-09

  1. ๋ฐฑ์—”๋“œ ๋ชจ๋ธ๋ง ๋ฐ ํ”„๋ฆฌ์ฆˆ๋งˆ ํ†ต์ผ ํŒ€์› ์ง„ํ–‰
  2. ๋ฒก์—”๋“œ ์„œ๋ฒ„ ์ƒ์„ฑ ๋ฐ ๋ ˆ์ด์–ด๋“œ ํŒจํ„ด ์ œ์ž‘
  3. ๋ฒก์—”๋“œ ๊ด€๋ จ ๊ฐ•์˜ 2๊ฐœ ๋“ฃ๊ธฐ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ