This file contains hidden or 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
| function CustomInput({ value, onChange, defaultValue }) { | |
| const inputRef = useRef(); | |
| return ( | |
| <input | |
| value={value} | |
| onChange={onChange} | |
| ref={inputRef} | |
| defaultValue={defaultValue} | |
| /> | |
| ); |
This file contains hidden or 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
| const [val, setVal] = useState(""); | |
| return ( | |
| <input value={val} onChange={e => setVal(e.target.value)} /> | |
| ); |
This file contains hidden or 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
| <input value={stateValue} onChange={...} /> |
This file contains hidden or 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
| <input defaultValue="Initial value" /> |
This file contains hidden or 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
| const inputRef = useRef(); | |
| <input ref={inputRef} /> |
This file contains hidden or 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
| function handleSubmit() { | |
| if (inputRef.current.value.includes("@")) { | |
| // Valid email | |
| } else { | |
| // Invalid | |
| } | |
| } |
This file contains hidden or 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
| <input | |
| value={email} | |
| onChange={e => setEmail(e.target.value)} | |
| required | |
| /> |
This file contains hidden or 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, { useRef } from "react"; | |
| function EmailInput() { | |
| const inputRef = useRef(); | |
| function handleSubmit() { | |
| alert(inputRef.current.value); | |
| } | |
| return ( | |
| <> | |
| <input ref={inputRef} /> | |
| <button onClick={handleSubmit}>Submit</button> |
This file contains hidden or 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 } from "react"; | |
| function NameInput() { | |
| const [name, setName] = useState(""); | |
| return ( | |
| <input value={name} onChange={e => setName(e.target.value)} /> | |
| ); | |
| } |
This file contains hidden or 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
| // Using spread instead of apply | |
| let args = [1, 2, 3]; | |
| someFunc(...args); // No need for apply! | |
| // Arrow functions for keeping 'this' | |
| const buttons = document.querySelectorAll('.btn'); | |
| buttons.forEach(btn => btn.addEventListener('click', () => handleClick())); |
NewerOlder