React Design Patterns [Container] 2.5 -2.6 ResourceLoader DataSurce.js

金秀·2022년 6월 9일
0

매일 리액트

목록 보기
13/15

09/06/2022

  • Refactor userLoader => to make a reusable component

ResourceLoader.js

  1. create ResourceLoader.js
  • instead of user prop, add resourceUrl , resourceName
  1. change state user => state 상태로

  2. endpoint => resourceUrl

  3. add setState to reflect the change

  4. [] => inside the useEffect array, add resourceUrl

  5. replace user props to state(current value of the state)

afterwards


App.js 확인

  1. import ResourceLoader
  2. replace UserLoader with ResourceLoader



=> 비슷해보이지만, a lot more flexible!🎊
instead of user, product 도 넣을 수 있음


!!!

default value 넣거나 && 나 conditional로 코드 짜기!!


2.6 DataSource Component

ONE STEP FURTHER!

  • replace axios call with a function prop

DataSource.js

App.js

  • pass the axios call from ResourceLaoder.js 함수 props으로
profile
기록하기

0개의 댓글

Powered by GraphCDN, the GraphQL CDN