Listing 7-7: Using an event handler function in a function component
You're searching for:
import {useState} from 'react';
function Search(props){
const [term,setTerm] = useState('');
const updateTerm = (searchTerm)=>{
setTerm(searchTerm);
}
return(
<>
<input type="text" value={term} onChange={(e)=>{updateTerm(e.target.value)}} /><br />
You're searching for: {term}
</>
);
}
export default Search;
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.