27
loading...
This website collects cookies to deliver better user experience
npx create-react-app react-live-search
cd spotify-react && yarn start
you jump into the projects directy and start the development server which then runs at http://localhost:3000
by default.(If for whatever reason the port is not 3000 make sure to change the redirect url in your spotify app settings.)
App.css
and in App.js
remove the imports as well as the html inside of the div with className App
. App.js
will look similar to this:import './App.css';
function App() {
return (
<div className="App">
</div>
);
}
export default App;
axios
which will handle our HTTP request with following commandyarn add axios
import axios from 'axios";
useState
Hook. So let's import it and create a new state variable characters
:import {useState} from 'react';
const [query, setQuery] = useState("")
useEffect
hook. So import that as well from react.App.js
should now look like this:import './App.css';
import {useEffect, useState} from 'react';
import axios from 'axios';
function App() {
const [characters, setCharacters] = useState([])
useEffect(() => {
}, [])
return (
<div className="App">
</div>
);
}
export default App;
fetchData
function which performs async get Request to the API Endpoint and gets the wanted data. We wrap our code inside of a try/catch block to handle possible errors.const fetchData = async () => {
try {
const {data} = await axios.get(`https://rickandmortyapi.com/api/character/`)
setCharacters(data.results)
} catch (error) {
console.error(error)
}
}
fetchData()
div
and inside of it we are going to map over the characters Array and dispaly the characters name as well as the image.<div className="results">
{characters.map(character => (
<div key={character.id}>
<img src={character.image} alt={character.name}/>
{character.name}
</div
))}
</div>
App.css
..results {
display: grid;
gap: 15px;
grid-template-columns: repeat(4, 1fr);
max-width: 1200px;
margin-left: auto;
margin-right: auto;
margin-top: 50px;
}
.search {
background-color: darkgray;
text-align: center;
padding: 15px;
}
.input {
font-size: 25px;
}
http://localhost:3000
you should be able to see the data we just fetched. Now we are ready for the last step.const [query, setQuery] = useState("")
onChange
event which triggers the state update. The value of the input field needs to contain the query states value:<div className="search">
<input type="text"
placeholder={"Search Character"}
className={"input"}
onChange={event => setQuery(event.target.value)}
value={query}
/>
</div>
const {data} = await axios.get(`https://rickandmortyapi.com/api/character/?name=${query}`)
fetchData
function will not be called again if we start typing our search query. To fix that we need to add query
as an dependancy to the useEffect hook. So your useEffect
will now look like this:useEffect(() => {
const fetchData = async () => {
try {
const {data} = await axios.get(`https://rickandmortyapi.com/api/character/?name=${query}`)
setCharacters(data.results)
} catch (error) {
console.error(error)
}
}
fetchData()
}, [query])
http://localhost:3000
and start typing inside your input field. The results now should update immediatly.