Skip to content

Instantly share code, notes, and snippets.

@simbathesailor
Last active June 21, 2020 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simbathesailor/43c67b031a14297c19bbe7e21d9f4c76 to your computer and use it in GitHub Desktop.
Save simbathesailor/43c67b031a14297c19bbe7e21d9f4c76 to your computer and use it in GitHub Desktop.
controlled-component-with-hooks1.jsx
function SearchAndResultView({ search, setSearch, result }) {
return (
<Modal>
<SearchBox search={search} setSearch={setSearch} />
<ResultContainer result={result} />
</Modal>
);
}
function AnyofyourComponent() {
const [search, setSearch] = React.useState("");
const [result, setResult] = React.useState([]);
async function onSearch(e) {
const value = e.target.value;
setSearch(value);
// do some API call
const res = await fetch("https://random-api");
setResult(res.data);
}
return (
<SearchAndResultView
search={search}
setSearch={setSearch}
result={result}
/>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment