π€ μμ§log π€
[REACT] Naver μ± κ²μ λ³Έλ¬Έ
π£ 1. Router.js μμ±
import { BrowserRouter, Routes, Route } from 'react-router-dom'
import Main from './components/pages/Main'
import Movie from './components/pages/Movie'
import Book from './components/pages/Book'
const Router = () => {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Main />} />
<Route path="/movie" element={<Movie />} />
<Route path="/book" element={<Book />} />
</Routes>
</BrowserRouter>
)
}
export default Router
π£ 2. Main.jsx
import { Link } from 'react-router-dom'
import styled from 'styled-components'
const Main = () => {
return (
<Wrapper>
<PageTitle>μν π¬ & μ±
π κ²μ</PageTitle>
<Link to="/Movie">
<Button>μν π¬</Button>
</Link>
<Link to="/Book">
<Button>μ±
π</Button>
</Link>
</Wrapper>
)
}
const Wrapper = styled.div`
padding: 15px;
`
const PageTitle = styled.h2`
text-align: center;
`
const Button = styled.button`
width: 100%;
margin: 10px 0;
height: 100px;
font-size: 24px;
border: none;
border-radius: 4px;
`
export default Main
π£ 3. Book.jsx
import { useState } from 'react'
import styled from 'styled-components'
import { getBookList } from '../../apis'
import BookList from '../organisms/BookList'
const Book = () => {
const [text, setText] = useState('')
const [bookList, setBookList] = useState([])
const handleSubmit = async (e) => {
e.preventDefault()
const { items } = await getBookList({ query: text })
setBookList(items)
}
return (
<Wrapper>
<PageTitle>π BOOK π</PageTitle>
<Form onSubmit={handleSubmit}>
<InputText
placeholder="search"
value={text}
onChange={(e) => setText(e.target.value)}
/>
<BtnSubmit>κ²μ</BtnSubmit>
</Form>
<BookList data={bookList} />
</Wrapper>
)
}
const Wrapper = styled.div``
const PageTitle = styled.h2``
const Form = styled.form`
display: flex;
padding: 15px;
`
const InputText = styled.input`
flex: 1;
margin-right: 15px;
`
const BtnSubmit = styled.button``
export default Book
π£ 3-1. BookList.jsx
import styled from 'styled-components'
const BookList = ({ data }) => {
return (
<List>
{data.map(({ image, title }) => (
<Item key={image}>
<Thumbnail src={image} />
<Title dangerouslySetInnerHTML={{ __html: title }} />
</Item>
))}
</List>
)
}
const List = styled.div`
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 10px;
padding: 15px;
`
const Item = styled.div``
const Thumbnail = styled.img`
width: 100%;
`
const Title = styled.span``
export default BookList
π£ 3-2. src>apis>index.js νμΌμμ±
import axios from 'axios'
export const getBookList = async (params) => {
const { data } = await axios.get('/v1/search/book.json', {
headers: {
'X-Naver-Client-Id': 'ID',
'X-Naver-Client-Secret': 'PW',
},
params,
})
return data
}
π μλ²λ₯Ό λ§λ€μ μμΌλ package.json μμ
"proxy": "https://openapi.naver.com" μΆκ°ν΄μ£ΌκΈ°
proxy λ κ°λ°κ³Όμ μμλ§ μ¬μ©κ°λ₯. λ°°ν¬ ν λλ μλ² νμν¨.
(μ¦, μ°μ΅λλ§ μ¬μ©ν μ μμ) π
728x90
'π React' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[REACT] Pagination κ³Ό Querystring (0) | 2022.10.06 |
---|---|
[REACT] Instagram _ Log-In (0) | 2022.10.06 |
[REACT] Naver μν κ²μ (0) | 2022.10.06 |
[REACT] Modal (0) | 2022.10.06 |
[REACT] DropDown (0) | 2022.10.06 |