๐Ÿ“— React

[React] ํ”„๋กœ์ ํŠธ GitHub Pages ๋ฐฐํฌํ•˜๊ธฐ + Axios ๋ฅผํ†ตํ•ด json data ๊ฐ€์ ธ์˜ค๊ธฐ

Tamii 2021. 3. 17. 17:39
๋ฐ˜์‘ํ˜•

AXIOS

fetch์œ„์— ์žˆ๋Š” ์ž‘์€ layer

// asios ์„ค์น˜
npm i axios

data fetchํ•˜๊ธฐ

๋ณดํ†ต Fetch ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ data๋ฅผ load ํ•˜๋””๋งŒ

asios๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž

 

class App extends React.Component{
  state={
    isLoading: true,
    movies:[]
  };

  getMovies = async() =>{
    const movies = await axios.get("https://yts-proxy.now.sh/list_movies.json");
    console.log(movies);

  };
  componentDidMount(){
    this.getMovies();
  }
  render(){
    const {isLoading} = this.state;
    return (
      <div>
        {isLoading? "Loading...":"We are ready"}
      </div>

    );
  }
}

 

 

data console๋กœ ์ฐ์€ ๋ชจ์Šต

movies ์•ˆ์˜ data ์•ˆ์˜ data ์•ˆ์˜ movies

const movies = await axios.get("https://yts-proxy.now.sh/list_movies.json");
console.log(movies.data.data.movies);
 
 
const {data:{data:{movies}}} = await axios.get("https://yts-proxy.now.sh/list_movies.json");
console.log(movies);

 

 

์ด์ œ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋กœ setSate( {  } ) ํ•œ ํ›„  ์›ํ•˜๋Š”๋Œ€๋กœ rendering ํ•˜๊ธฐ

//๊ฐ€์ ธ์˜จ data๋กœ setState()
  getMovies = async() =>{
    const {
      data:{
        data: {movies}
      }
    } = await axios.get(
        "https://yts-proxy.now.sh/list_movies.json?sort_by=rating");
    this.setState({movies, isLoading:false})

  };

 


 

------>

์ดํ›„ render() ์•ˆ์— html ์š”์†Œ์™€ className์„ ๋„ฃ๊ณ  

CSS ์ž‘์—…์„ ์™„๋ฃŒํ•˜๋ฉด localhost:3000์—์„œ ์™„์„ฑ๋œ ํŽ˜์ด์ง€ ํ™•์ธ ๊ฐ€๋Šฅ! 

 

 

 

์ด์ œ ์„œ๋ฒ„์— ์—ฐ๊ฒฐํ•˜์—ฌ local์ด ์•„๋‹Œ ์›น์‚ฌ์ดํŠธ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ณด์ž

 


gh-pages

๋‚˜์˜ ์›น์‚ฌ์ดํŠธ๋ฅผ github page ๋„๋ฉ”์ธ์— ๋‚˜ํƒ€๋‚˜๊ฒŒํ•จ 

gituhb์—์„œ ๋ฌด๋ฃŒ๋กœ static pages html,css,javascript ์˜ ์›น์‚ฌ์ดํŠธ๋ฅผ ํ‘œํ˜„ํ•ด์คŒ 

 

1)  gh-pages ์„ค์น˜

npm i gh-pages

 

2)  Package.json ์„ค์ •

2-1) homepage ์ƒ์„ฑ

"homepage" : " https://{ github ๊ณ„์ •์ด๋ฆ„ }.github.io/{ ๊นƒํ—ˆ๋ธŒ ๋‚ด ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ }"
"homepage": "https://ink-0.github.io/movie_app/"

2-2) scripts ์ถ”๊ฐ€

deploy  , predeploy

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "deploy":"gh-pages -d build",
    "predeploy":"npm run build"
  },

 

3) build ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ 

(๋‚ด๊ฐ€ ์ง  CSS ๋“ฑ์ด ์š”์•ฝ๋œ ํด๋”๊ฐ€ ์ƒ์„ฑ๋จ)

npm run build

 

4) deploy ์‹คํ–‰

npm run deploy

<๋™์ž‘ ์ˆœ์„œ> 

deploy๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ์ž๋™์œผ๋กœ predeploy๋ฅผ ๋จผ์ € ๋™์ž‘์‹œํ‚ด 

4-1) predeploy  

npm run build ("build "์‹คํ–‰)    ->   react-scripts build("build ๋””๋ ‰ํ† ๋ฆฌ ์ƒ์„ฑ")

 

4-2) deploy

gh-pages -d build (gh-pages ์— build ๋””๋ ‰ํ† ๋ฆฌ ์ „๋‹ฌ )   ->   published 

 

5) ์‚ฌ์ดํŠธ published ํ™•์ธ 

 

 

 


 

โš ๏ธ ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค๋ฉด ?

 

์—ฌ๊ธฐ์„œ ๋‚˜๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋Š”๋ฐ ์ผ๋‹ค ์ฒซ๋ฒˆ์งธ,

์˜ค๋ฅ˜1) ์‚ฌ์ดํŠธ๋ช… ์ œ๋Œ€๋กœ ํ™•์ธํ•˜๊ธฐ 

fe-w6-free-style/movie_app ์—์„œ ๊ตฌํ˜„์ค‘

๋‚˜๋Š” fe-w6-free-style (  ๊ณต๋™ github repository ์—์„œ ๋‚ด branch ๋งŒ fork ํ•ด์˜จ origin repository ) ์—์„œ 

movie_app์ด๋ผ๋Š” ํด๋”๋ฅผ ๋งŒ๋“ค์–ด ๊ตฌํ˜„์ค‘์ด์—ˆ๋‹ค.

 

github page๋Š” repository๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒ์„ฑ๋˜๋Š”๋“ฏ ํ•œ๋ฐ 

"homepage": "https://ink-0.github.io/movie_app/"

๋‚˜๋Š”hoempage ๋ฅผ ์ด๋ ‡๊ฒŒ ํ”„๋กœ์ ํŠธ ์•ˆ์˜ ๋””๋ ‰ํ† ๋ฆฌ๋ช…(movie_app) ์œผ๋กœ ์ง€์ •ํ•ด๋†“๊ณ  ํ•ด๋‹น ์‚ฌ์ดํŠธ๋ฅผ ๊ฐ€์„œ ํ—ค๋ฉ”๊ณ  ์žˆ์—ˆ๋‹ค.

 

gh-pages ๊ฐ€ ์–ด๋””์„œ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€๋Š”

ํ•ด๋‹น repository์˜ githubํŽ˜์ด์ง€ -> GitHub  pages  -> Source   ์—์„œ ํ™•์ธํ•˜์ž

Branch : gh-pages๋กœ ๋˜์–ด์žˆ๋Š”์ง€๋„ ํ™•์ธ 

 

 

์˜ค๋ฅ˜2) git-pages ๋ฒ„์ „ ํ™•์ธํ•˜๊ธฐ

 

git-pages ๋ฒ„์ „ ๋•Œ๋ฌธ์— ๋‚˜์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

npm i gh-pages@2.0.1

์ด ๋ฒ„์ „์œผ๋กœ ๋‹ค์‹œ ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•˜๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ํŽ˜์ด์ง€๊ฐ€ ๋‚˜์˜จ ๊ฑธ ํ™•์ธํ–ˆ๋‹ค.

 

 

 

 

 

 

 

 

ํœด ์–ด์จ‹๋“  ์„ฑ๊ณต!!