ReactJS로 영화 웹 서비스 만들기 2

2️⃣

🖥 MAKING THE MOVIE APP - 4

4 - 0. Fetching Movies from API

getMovies = async () => {
    // 데이터를 받아오기 위한 비동기 처리
    const movies = await axios.get("https://yts-proxy.now.sh/list_movies.json");
}

componentDidMount() {
    this.getMovies();
}

4 - 1. Rendering the Movies

// src/App.js
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})
    //  this.setState({movies(state.movies):movies(위에서 받아온 데이터의 movies)})
}

// src/Moive.js ( state 가 필요없기때문에 함수 컴포넌트를 이용)
import React from 'react';
import PropTyeps from 'prop-types';

function Movie({id, year, title, summary, poster}){
    return (
        <h4>{title}</h4>
    )
}

Movie.prototype = {
    id: PropTyeps.number.isRequired,
    year: PropTyeps.string.isRequired,
    title: PropTyeps.string.isRequired,
    summary: PropTyeps.string.isRequired ,
    poster: PropTyeps.string.isRequired
}

export default Movie;

4 - 2. Styling the Movies

4 - 3. Adding Genres

4 - 5. Cutting the summary

🖥 CONCLUSIONS - 5

5 - 0. Deploying to Github Pages

5 - 1. Are we done?

🖥 ROUTING BONUS - 6

6 - 0. Getting Ready for the Router

6 - 1. Building the Router

//  src/App.js
import React from 'react';
import { HashRouter, Route  } from 'react-router-dom'
import About from './routes/About'

function App(){
  return (
    <HashRouter>
      {/* Route 안에 중요한 props 2개가 들어간다. */}
      {/* 1. 랜더링할 스크린 */}
      {/* 2. 뭘 할지 정해주는 props */}
      <Route path="/" component={Home} />
      <Route path="/about" component={About} />
      {/* 만약 내가  path="/about" 로 가게되면 component={About}을 보여줘 라는 뜻 */}
    </HashRouter>
  );
} 

export default App;
function App(){
  return (
    <HashRouter>
      <Route path="/" exact={true} component={Home} />
      {/* only url path="/" 일때만 Home 이 렌더링 되게  */}
      <Route path="/about" component={About} />
    </HashRouter>
  );
} 

6 - 2. Building the Navigation

// component/Navigation.js
import React from 'react'
import { Link } from 'react-router-dom'

function Navigation(){
    return (
      <div>
        {/* <a href="/">HOME</a>
        <a href="/about">ABOUT</a> */}
        {/* a  href 를 사용하면 페이지를 아예 새로고침해버린다. */}
        <Link to="/">HOME</Link>
        <Link to="/about">ABOUT</Link>
      </div>
    );
}

export default Navigation;

// src/App.js
function App(){
  return (
    <HashRouter>
      <Navigation />
      {/*  Navigation 안에서 사용되는 Link 는 Router 밖에서는 사용될 수 없다.*/}
      <Route path="/" exact={true} component={Home} />
      <Route path="/about" component={About} />
    </HashRouter>
  );
} 

6 - 3. Sharing Props Between Routes

6 - 4. Redirecting

import React from 'react';

class Detail extends React.Component{
    componentDidMount(){
        const { location, history } = this.props;
        if(location.state === undefined){
            history.push("/")
            // 카드를 클릭해서 들어온게 아니라면 리다이렉트 시킴( 홈 화면으로 )
        }
        console.log(location.state); 
    }

    render(){
        const {location} = this.props
        if(location.state){
            return <span>{location.state.title}</span>;
        } else return null 
    }
}

export default Detail;