Last active
July 21, 2020 00:39
-
-
Save reireynoso/f373a0ad80f6a455cb8fa0089c256e9a to your computer and use it in GitHub Desktop.
useRef for storing mutable information
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, useState} from 'react' | |
const MessageInputComponent = () => { | |
const [message, setMessage] = useState("") | |
const sentMessage = useRef(0); | |
const sendMessage = () => { | |
if(sentMessage.current === 3){ | |
return alert("Message Limit Reached") | |
} | |
sentMessage.current += 1 | |
//code to handle sending message | |
} | |
return( | |
<div> | |
<input onChange = {() => setMessage(e.target.value)} value={message}/> | |
<button onClick={sendMessage}>Send</button> | |
</div> | |
) | |
} | |
export default MessageInputComponent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment