-
Notifications
You must be signed in to change notification settings - Fork 34
3week #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 3week
Are you sure you want to change the base?
3week #75
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import React, { Component } from 'react'; | ||
| import axios from 'axios' | ||
| import ContentList from '../component/contentList/ContentList.js' | ||
|
|
||
| export default class Game extends Component { | ||
|
|
||
| constructor(props) { | ||
| super(props); | ||
| this.state = { | ||
| contents : [] // 하단의 리스트에 들어갈 콘텐츠를 가지는 state | ||
| }; | ||
| } | ||
|
|
||
| // 서버로 부터 받은 데이터를 내가 원하는 형태로 변경 하는 함수 | ||
| // {id: , name: } 형식으로 모든 데이터들을 변환 | ||
| setContents = (data) => { | ||
| let list = [] | ||
| data.items.forEach((item, index) => { | ||
| list.push({id:item.id,name:item.snippet.title}) | ||
| }) | ||
| return list | ||
| } | ||
|
|
||
| //컴포넌트 렌더링이 완료된 후 유튜브에서 데이터 불러옴 | ||
| componentDidMount() { | ||
| this.fetchYoutube() | ||
| } | ||
|
|
||
| //유튜브에 ajax 통신을 해서 데이터를 불러 오는 함수 | ||
| fetchYoutube = () => { | ||
| //axios를 이용해서 유튜브에 영상 목록을 달라고 요청 | ||
| axios.get('https://www.googleapis.com/youtube/v3/search?q=게임&part=snippet&chart=mostPopular&key=AIzaSyASlUxKp9-sRxTnJUghY0f9D0ia0Iqp91U&maxResults=30') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. q, key, maxResult값은 언제든 변경할수 있는 값입니다. 그리고 문자열과 변수를 혼용해야 할때 Template literals이라는 문법을 사용해서 사용가능합니다. |
||
| .then(({data}) => {//유튜브로 부터 요청한 데이터를 전달 받으면 then으로 데이터를 받음 | ||
| //디스트럭쳐링을 통해서 유튜브로부터 받은 데이터중에서 data만 가져옴 | ||
|
|
||
| const list = this.setContents(data)//받아온 데이터를 내가 원하는 형태로 가공 | ||
|
|
||
| this.setState({// 가공한 데이터로 지금 바로 실행할 데이터와, 목록에 보여줄 데이터를 state에 저장 | ||
| contents:list.slice(1,list.length),//slice 함수는 배열형 데이터를 첫번째인자부터 두번째인자 전까지를 반환해주는 함수 입니다. | ||
| fullContent:list[0]//가져온 데이터중 첫번째 데이터를 화면에서 실행 시킵니다. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fullContent는 사용하지 않으므로 삭제해주시면 좋습니다! |
||
| }) | ||
| }) | ||
| } | ||
| setContents = (data) => { | ||
| let list = [] | ||
| data.items.forEach((item, index) => { | ||
| if(item.id.videoId) { | ||
| list.push({id:item.id.videoId,name:item.snippet.title}) | ||
| } | ||
| }) | ||
| return list | ||
| } | ||
|
|
||
| render() { | ||
| return ( | ||
| <div> | ||
| <ContentList contents={this.state.contents} /> | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import React, { Component } from 'react'; | ||
| import axios from 'axios' | ||
| import ContentList from '../component/contentList/ContentList.js' | ||
|
|
||
| export default class Search extends Component { | ||
|
|
||
| state = { | ||
| keyword:"", | ||
| contents: [] | ||
| } | ||
|
|
||
| handleChange = (e) => { | ||
| this.setState({keyword:e.target.value}) | ||
| } | ||
|
|
||
| handleSubmit = (e) => { | ||
| this.fetchSearch(this.state.keyword) | ||
| e.preventDefault() | ||
| } | ||
| // preventDefault : event 하지마! | ||
|
|
||
| setContents = (data) => { | ||
| let list = [] | ||
| data.items.forEach((item, index) => { | ||
| if(item.id.videoId) { | ||
| list.push({id:item.id.videoId,name:item.snippet.title}) | ||
| } | ||
| }) | ||
| return list | ||
| } | ||
|
|
||
| fetchSearch = async (keyword) => { | ||
| const maxResults = 30 | ||
| const token = 'AIzaSyASlUxKp9-sRxTnJUghY0f9D0ia0Iqp91U' | ||
| // var는 너무 글로벌해서 관리가 어려워서 사용 x, let 보다 const 를 자주 사용 | ||
| try { | ||
| const {data} = await axios.get('https://www.googleapis.com/youtube/v3/search?q='+ keyword +'&part=snippet&key='+ token +'&maxResults='+maxResults) | ||
| // 여러 값들 중에서 data 부분만 저장하겠다 | ||
| this.setState({contents: this.setContents(data)}) | ||
| } catch { | ||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| render() { | ||
| return ( | ||
| <div> | ||
| <form onSubmit={this.handleSubmit}> | ||
| <input type='text' value={this.state.keyword} onChange={this.handleChange} /> | ||
| </form> | ||
| <ContentList contents={this.state.contents} /> | ||
| </div> | ||
| ) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
state를 사용하기 위한 방법으로 constructor에서 사용할수도 있지만
class property initializers를 이용해서 좀더 간단하게 state를 선언 하실수도 있습니다!
https://react.christmas/2017/17
ex) state = { fullcontent: {}, contents: []}