diff --git a/react-complete-guide/react-complete-notes.md b/react-complete-guide/react-complete-notes.md index 755abed..e911ffa 100644 --- a/react-complete-guide/react-complete-notes.md +++ b/react-complete-guide/react-complete-notes.md @@ -73,3 +73,79 @@ React adds its own event attribute to use in JSX elements Each event starts with "on" and pops in Intellisense +``` + + +const clickHandler = () => { + setTitle('Updated!'); + console.log(title); + }; +``` + +### State + +Uses a variable and a method to store and update information on the screen + +Work with state using the built-in method useState() + +``` +const [title, setTitle] = useState(props.title); + +const clickHandler = () => { + setTitle('Updated!'); + console.log(title); +}; +``` + +If your form has a lot of fields to track there are two ways of using state. + +1. The more common approach is to have everything outlined individually + +``` +const [enteredTitle, setEnteredTitle] = useState(''); +const [enteredAmount, setEnteredAmount] = useState(''); +const [enteredDate, setEnteredDate] = useState(''); + +const titleChangeHandler = (event) => { + setEnteredTitle(event.target.value); +}; +const amountChangeHandler = (event) => { + setEnteredAmount(event.target.value); +}; +const dateChangeHandler = (event) => { + setEnteredDate(event.target.value); +}; +``` + +2. The less common way is to use an object +``` +const [userInput, setUserInput] = useState({ + enteredTitle: '', + enteredAmount: '', + enteredDate: '' +}); + +const titleChangeHandler = (event) => { + setUserInput((prevState) => { + return {...prevState, enteredTitle: event.target.value}; + }); +}; +const amountChangeHandler = (event) => { + setUserInput((prevState) => { + return {...prevState, enteredAmount: event.target.value}; + }); +}; +const dateChangeHandler = (event) => { + setUserInput((prevState) => { + return {...prevState, enteredDate: event.target.value}; + }); +}; +``` + +### Form Handlers + +``` +const submitHandler = (event) => {}; + +return