Guest User

Untitled

a guest
Nov 8th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. const [category, setCategory] = useState([]);
  2. const [categoryId, setCategoryId] = useState([]);
  3. const [categorySub, setCategorySub] = useState([]);
  4. const [isActive, setActive] = useState(false);
  5.  
  6. //Взять id
  7. const handleClick = (event) =>{
  8. setCategoryId(event.currentTarget.id)
  9. }
  10.  
  11. // Обычный запрос на все категории
  12.  
  13. useEffect(() => {
  14. fetch('http://api.tmweb.ru/category?filter[depth]=1&filter[type]=0',{
  15. headers: {
  16. 'Accept': 'application/json',
  17. 'Authorization': token,
  18. }
  19. })
  20. .then(res => res.json())
  21. .then((result) => setCategory(result.data),
  22. )
  23. }, [])
  24.  
  25. // Запрос при клике с хуком который передает id
  26.  
  27. let getDataCategory = async () => {
  28. await fetch(`http://api.tmweb.ru/category?filter[parent_id][in]=${categoryId}&filter[type]=0`,{
  29. headers: {
  30. 'Accept': 'application/json',
  31. 'Authorization': token,
  32. }
  33. })
  34. .then(res => res.json())
  35. .then((result) => setCategorySub(result.data),
  36. )
  37. }
  38.  
  39. // С инпута берем id, при онклике по элементу делаем запрос.
  40. {category.map(item =>
  41. <div onClick={getDataCategory} className="equipment__special__check" key={item.id}>
  42.  
  43. <input type="checkbox" onClick={handleClick} id={item.id} />
  44. <label htmlFor={item.id} className="product__wrap">
  45. <div className="img">
  46. <img src={url + item.image} alt="" />
  47. </div>
  48. <p className="title">{item.title}</p>
  49. </label>
  50. </div>
  51. )}
Advertisement
Add Comment
Please, Sign In to add comment