Skip to content

Instantly share code, notes, and snippets.

@reireynoso
Last active July 21, 2020 00:39
Show Gist options
  • Save reireynoso/f373a0ad80f6a455cb8fa0089c256e9a to your computer and use it in GitHub Desktop.
Save reireynoso/f373a0ad80f6a455cb8fa0089c256e9a to your computer and use it in GitHub Desktop.
useRef for storing mutable information
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