0

I am using use effect to fetch data from API in a React App

let [rows, setRows] = useState([]);

useEffect(()=>{
  console.log('use effect for first time')
  const url = "https://somepath.com/fff";
  fetch(url).then(response => response.json())
  .then(response=> setRows(response.json.Items))
},[])

After this rows looks like this.

[
  { name: "Jimmy", skills: ["Docker", "JavaScript", "Ruby"] },
  { name: "Dave", skills: ["AWS", "Python"] },
  { name: "Frankie", skills: ["Azure", "JavaScript"] },
  { name: "Liam", skills: ["Java", "JavaScript"] },
  { name: "Fred", skills: ["JavaScript", "AWS"] },
  { name: "Sara", skills: ["PHP", "AWS"] },
  { name: "Matt", skills: [".Net", "PHP", "Docker"] },
]

When I try to apply the filter then I do not get the correct result

useEffects(()=>{
const result = rows.filter(e=> e.skills.includes('JavaScript'));;
console.log(result);
},[xyz])

Also when I console Typeof rows I get "Object" and not an Array

creativated
  • 211
  • 3
  • 16
  • 3
    `Arrays` are object in JS... – DecPK Nov 24 '21 at 01:08
  • 1
    Where are you executing your _filter_ line? What result where you expecting? What result are you getting? – Phil Nov 24 '21 at 01:10
  • 2
    So technically arrays are objects in JS. Almost everything is. As for the filter issue, what do you mean "do not get correct result"? What incorrect output are you getting? – Jayce444 Nov 24 '21 at 01:11
  • I get an empty array however it should come with 4 items – creativated Nov 24 '21 at 01:14
  • This is a duplicate of either [useState set method not reflecting change immediately](https://stackoverflow.com/q/54069253/283366) or [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086/283366). Which one will be determined by how you answer my question – Phil Nov 24 '21 at 01:20
  • In another useEffect where I want to change the "rows" variable – creativated Nov 24 '21 at 01:20
  • 2
    This is like pulling teeth. Please [edit your question](https://stackoverflow.com/posts/70089666/edit) to show all the relevant code. You need to provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) when asking questions – Phil Nov 24 '21 at 01:21
  • 1) It's `useEffect`, not `useEffects`. 2) The dependencies for your second effect hook should include `rows` – Phil Nov 24 '21 at 01:26
  • The first problem I noticed is that you have useEffects with additional s at the end. – tarek hassan Nov 24 '21 at 01:31

0 Answers0