Created
August 22, 2020 04:06
-
-
Save jarodsim/60ed5281f88616cd1fe5e9cbe7acb578 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from "react"; | |
export default function App() { | |
const [text, setText] = useState(""); | |
const [data, setData] = useState([ | |
{ | |
id: 1, | |
name: "abacate" | |
}, | |
{ | |
id: 2, | |
name: "abacate" | |
}, | |
{ | |
id: 3, | |
name: "abobora" | |
}, | |
{ | |
id: 4, | |
name: "arroz" | |
} | |
]); | |
const [show, setShow] = useState([]); | |
useEffect(() => { | |
const result = data.filter((item) => item.name.indexOf(text) !== -1); | |
setShow(result); | |
}, [text, data]); | |
return ( | |
<div className="App"> | |
<h1>Busca</h1> | |
<input | |
type="text" | |
value={text} | |
onChange={(e) => setText(e.target.value)} | |
/> | |
{show.map((item) => ( | |
<p key={item.id}>{item.name}</p> | |
))} | |
</div> | |
); | |
} | |
// https://codesandbox.io/s/simples-busca-espontanea-react-x9ewu?file=/src/App.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gist testing