as you can see from my code I have some props({allRecipes}) fetched by Redux, I can display them with const mapRecipe =(), but I would like to filter them by a search bar, I think the solution would be the hook useEffect, but I can't go on,
useEffect(() =>{
const res = allRecipies.filter(el => el.name.toLowerCase().includes(searchTerm))
setSearchResults(res)},[searchTerm])
give to me error: allRecipies is null.
hope someone can point me on the right direction.
here the code:
const [searchTerm, setSearchTerm] = useState("");
const [searchResults, setSearchResults] = useState([]);
const handleChange = event => {
console.log("search bar",event.target.value)
setSearchTerm(event.target.value);
}
useEffect(() =>{
const res = allRecipies.filter(el => el.name.toLowerCase().includes(searchTerm))
setSearchResults(res)
},[searchTerm])
const mapRecipe =() =>{
if(!allRecipies){return<li>no fish</li>}
else{return allRecipies.map(el =>{
return (<div className="col s12 l4" key={el._id} >
<div className="card ">
<div style={{backgroundImage:`url(${staticImage})`,height:"200px",backgroundSize:"cover"}} className="card-image ">
<a className="btn-floating halfway-fab waves-effect waves-light btn-large lime darken-2"><i className="material-icons">clear</i></a>
</div>
<span className="card-title">{el.name}</span>
<div className="card-content">
<p>{el.listOfStages[0]}</p>
</div>
</div>
</div>)
})}
}
return (
<div>
<input type="text"
placeholder="search"
value={searchTerm}
onChange={handleChange}/>
<div className="row" >
{mapRecipe()}
</div>
</div>
)
}
function mapStateToProps(state){
console.log(state);
return state
}
export default connect(mapStateToProps)(Landing)
Use null propogation to get rid of that error:
useEffect(() =>{
const res = allRecipies?.filter(el => el.name.toLowerCase().includes(searchTerm))
setSearchResults(res)
},[searchTerm])
You can read more about it here : Null Propagation Operator in JavaScript
I would do this:-
detect incoming allRecipies with useEffect & apply default searchTerm:-
another useEffect for filtering searchTerm:-
// do search
const seacrh = (allRecipies, searchTerm) => {
return allRecipies.filter(el => el.name.toLowerCase().includes(searchTerm))
}
// run when 'allRecipies' present
useEffect(() => {
(() => {
if(allRecipes) {
setSearchResult(() => search(allRecipies, ''))
}
})()
}, [allRecipies])
// run when there's changes on 'searchTerm'
useEffect(() => {
(() => {
if(searchTerm) {
setSearchResult(() => search(allRecipies, searchTerm))
}
})()
}, [searchTerm])
Related
I have the following code:
function App() {
const [countries,setCountries]= useState([]);
const [search, setSearch] = useState('');
//Take data from API with useEffect, async/await and try/catch
useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.get('https://restcountries.com/v2/all');
setCountries(response.data);
} catch (error) {
console.error(error);
}
}
fetchData();
}, []);
const filteredCountries = countries.filter((country) =>
country.name.toLowerCase().includes(search.toLowerCase())
);
const handleSelect = (country) => {
setSearch(country.name);
}
return (
<>
<div>
<SearchBar onChange={(e)=> setSearch(e.target.value)} />
{
<ul className="list">
{search.length > 0 && filteredCountries.map((country) => (
<li key={country.name} onClick={() => handleSelect(country)}>
{country.name}
</li>
))}
</ul>
}
</div>
<div className="map-container">
</div>
</>
)
}
export default App;
The result is this:
List image
How can I select an item from the list, e.g. if I search for Ital, Italy appears and I would like to select it and have it appear in the search bar.
I would like to create a search bar to find a country and select it, it should appear in the search bar after being selected.
CodeSandBox Link: https://codesandbox.io/p/github/pierre1590/Population-Tracker/draft/gallant-gagarin?file=%2Fsrc%2Fcomponents%2FMap%2FMap.js
Add value={search} in your <SearchBar/> component.
eg: <SearchBar value={search} onChange={(e)=> setSearch(e.target.value)} />
Below is the full code (I've used a normal input tag in place of your SearchBar component)
import { useState, useEffect } from "react";
import axios from 'axios';
function App() {
const [countries,setCountries]= useState([]);
const [search, setSearch] = useState('');
console.log(search)
//Take data from API with useEffect, async/await and try/catch
useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.get('https://restcountries.com/v2/all');
setCountries(response.data);
} catch (error) {
console.error(error);
}
}
fetchData();
}, []);
const filteredCountries = countries.filter((country) =>
country.name.toLowerCase().includes(search.toLowerCase())
);
const handleSelect = (country) => {
setSearch(country.name);
}
return (
<>
<div>
<input value={search} onChange={(e)=> setSearch(e.target.value)} />
{
<ul className="list">
{search.length > 0 && filteredCountries.map((country) => (
<li key={country.name} onClick={() => handleSelect(country)}>
{country.name}
</li>
))}
</ul>
}
</div>
<div className="map-container">
</div>
</>
)
}
export default App;
CodeSandBox Link - https://codesandbox.io/s/enter-the-selected-item-in-the-search-bar-in-react-js-582rez?file=/src/App.js
import React, { useState, useEffect } from "react";
import "./style.css";
const getLocalItem = () => {
let list = localStorage.getItem("lists");
console.log(list);
if (list) {
return JSON.parse(list);
} else {
return [];
}
};
function App() {
const [text, setText] = useState("");
const [task, setTask] = useState(getLocalItem());
const changeText = (e) => {
setText(e.target.value);
};
const submitHandler = (e) => {
console.log("submited");
e.preventDefault();
setTask([...task, text]);
setText("");
};
const removeTask = (a) => {
const finalData = task.filter((curEle, index) => {
return index !== a;
});
setTask(finalData);
};
useEffect(() => {
localStorage.setItem("lists", JSON.stringify(task));
}, [task]);
return (
<>
<form onSubmit={submitHandler} className='form'>
<div className="action" >
<div >
<input
className="input"
type="text"
value={text}
onChange={changeText}
placeholder='add task...'
/>
</div>
<button type="submit" className="button" >
Add todo
</button>
</div>
<div className="listsData">
{task.map((value, index) => {
return (
<>
<div key={index}>
{value}
</div>
</>
);
})}
</div>
</form>
</>
);
}
export default App;
On adding each item I want a different color for each list. Currently, I am fetching list data from localstorage while fetching also it should remain same. which is working but the dynamic colors is what I need for each list. Any ideas or dynamic logics??
Let me know if u need more details regarding my code if u doont understand something
When I click on the Delete button, my code does not work. There could be a problem in the function handleRemove.
import React, { useState, useEffect } from 'react'
import axios from 'axios'
// API endPoint - Punk API
const API_URL = 'https://api.punkapi.com/v2/beers'
const List = () => {
const [drinks, setDrinks] = useState([])
const [searchTerm, setSearchTerm] = useState('')
const fetchData = async () => {
const { data } = await axios.get(API_URL)
setDrinks(data)
}
useEffect(() => {
fetchData()
}, [])
const handleRemove = (id) => {
let groupd = drinks
const newList = groupd.filter(group => group.id !== id)
setDrinks(newList)
}
return (
<div>
<div className="wrapper">
<div className="search__main">
<input type='text' placeholder="search..." onChange={e => {setSearchTerm(e.target.value)}}/>
</div>
</div>
<div className="wrapper">
<div className="search__box">
{drinks.filter((val) => {
if(searchTerm === ""){
return val
} else if(val.name.toLowerCase().includes(searchTerm.toLowerCase()) || val.description.toLowerCase().includes(searchTerm.toLowerCase())){
return val
}
}).map((drink, key) => {
return(
<div key={key} className="search__mini__box">
<div >
<img src={drink.image_url} alt="drink" className="search__img"/>
</div>
<h4>{drink.name}</h4>
<p>{drink.description}</p>
<button type="button" onClick={handleRemove(drink.id)}>
delete
</button>
</div>
)
})}
</div>
</div>
</div>
)
}
export default List
Since your handleRemove function call is within a return statement, you need to call the function like so:
onClick={() => handleRemove(drink.id)}
What happens is, the function is called immediately on render if done the way you've proposed in your question. We want the function to be called only when the button is clicked.
I am getting the nominee_name as the last span name even after clicking on the right span element. How can I get the correct span name from here <span className="countname" key={data.nomineename} ref={nominee_name} onClick={handleClick}>{data.nomineename}</span>.
The above span is iterated based on the data received.
import React, { useRef, useEffect, useState } from "react";
import Axios from "axios";
const Dashboard = props => {
const [nominationCount, setNominationCount] = useState([]);
const [nameText, setNameText] = useState("");
let nominee_name = useRef(null);
const isMounted = useRef(false);
useEffect(() => {
isMounted.current = true;
return () => isMounted.current = false;
}, []);
useEffect(() => {
const fetchData = async () => {
try {
const res = await Axios.get('http://localhost:8000/service/nominationcount');
if (isMounted.current) {
setNominationCount(res.data);
console.log("Nomination count data from server :" + res.data);
}
} catch (e) {
console.log(e);
}
}
fetchData();
}, []);
const handleClick = () => {
setNameText(nominee_name.current.outerText);
}
return (
<div className="space_1 tile">
<h3>Nominations Count</h3>
<div className="grid-container">
{
!nominationCount.length && (<div className="nonominationdata">No nominations count to display !</div>)
}
{
nominationCount.map(data => (
<div key={data.id}>
<div onClick={() => {setOpen(!open); }} className="count badge" >
<span className="badgenumber" value={data.count} key={data.count}>{data.EmailCount}</span>
<span className="countname" key={data.nomineename} ref={nominee_name} onClick={handleClick}>{data.nomineename}</span>
</div>
</div>
))
}
</div>
</div>
<Modal
open={open}
onClose={() => {
setOpen(false);
}}
className={classes.modal}>
<form className={classes.form}>
<label className={classes.label}>Confirm winner {nameText}</label>
<input className={classes.submit} type="submit" value="Confirm" />
</form>
</Modal>
)
}
not sure you want to use ref here.
Just pass the name into your click handler:
(
<span className="countname" key={data.nomineename}
onClick={()=>setNameText(data.nomineename)}>{data.nomineename}</span>
)
I am trying to organize my code order to handle feed as feed.* based on my endpoint API, but however react doesn't allow me to directly send functions into component, but I want something similar to feed.results, feed. count
const [initialized, setIntialized] = useState(false);
const [feed, setFeed] = useState([]);
const browserFeed = async () => {
const response = await browse();
setFeed(response.results);
setIntialized(true);
};
useEffect(() => {
if (!initialized) {
browserFeed();
}
});
export const browse = () => {
return api.get('xxxxxxxx')
.then(function(response){
return response.data // returns .count , .next, .previous, and .results
})
.catch(function(error){
console.log(error);
});
}
<div className="searched-jobs">
<div className="searched-bar">
<div className="searched-show">Showing {feed.count}</div>
<div className="searched-sort">Sort by: <span className="post-time">Newest Post </span><span className="menu-icon">▼</span></div>
</div>
<div className="job-overview">
<div className="job-overview-cards">
<FeedsList feeds={feed} />
<div class="job-card-buttons">
<button class="search-buttons card-buttons-msg">Back</button>
<button class="search-buttons card-buttons">Next</button>
</div>
</div>
</div>
</div>
If it is pagination you are trying to handle here is one solution:
async function fetchFeed(page) {
return api.get(`https://example.com/feed?page=${page}`);
}
const MyComponent = () => {
const [currentPage, setCurrentPage] = useState(1);
const [feed, setFeed] = useState([]);
// Fetch on first render
useEffect(() => {
fetchFeed(1).then((data) => setFeed(data));
}, []);
// Update feed if the user changes the page
useEffect(() => {
fetchFeed(currentPage).then((data) => setFeed(data));
}, [currentPage]);
const isFirstPage = currentPage === 1;
return (
<>
<FeedsList feeds={feed} />
{isFirstPage && (
<button onClick={() => setCurrentPage(currentPage - 1)}>Back</button>
)}
<button Click={() => setCurrentPage(currentPage + 1)}>Next</button>
</>
);
};