0

Below is my code with a search input hoos and I can't identify why it isn't working.

import Herois from './json/videos.json'

function App() {
  const [valueInput, setValueInput] = useState('')
  const [newArray, setNewArray] = useState([])

  useEffect(() => {
    const results = Herois.filter((i) => {
      i.title.toLowerCase().includes(valueInput.toLowerCase())
    })
    
    setNewArray(results)
    console.log(newArray)
  }, [valueInput])
}

is always becoming an empty array

2
  • console.log(results) ta mostrando algo? Commented Sep 25, 2020 at 0:44
  • @matmahnke - after update that was suggested below, Returns a value if I give console.log in the results, if I give console.log in newArray it returns empty Commented Sep 25, 2020 at 0:47

1 Answer 1

1
const results = Herois.filter((i) => {
 // you have to return the something here
 return i.title.toLowerCase().includes(valueInput.toLowerCase())
})

or

const results = Herois.filter((i) => (i.title.toLowerCase().includes(valueInput.toLowerCase())
 ))
Sign up to request clarification or add additional context in comments.

2 Comments

Returns a value if I give console.log in results, if I give console.log in newArray it returns empty
setNewArray is an async function means it will not block the next piece of code(console.log(newArray)) until it finishes executing. if you want to test newArray empty or not use this: setTimeout(()=>{console.log(newArray)},1000)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.