Skip to content

Instantly share code, notes, and snippets.

@jarodsim
Created August 22, 2020 04:06
Show Gist options
  • Save jarodsim/60ed5281f88616cd1fe5e9cbe7acb578 to your computer and use it in GitHub Desktop.
Save jarodsim/60ed5281f88616cd1fe5e9cbe7acb578 to your computer and use it in GitHub Desktop.
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
@aniketkumar2808
Copy link

Gist testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment