0
I am creating a character counter that needs to be updated according to the amount of characters typed in the text field. The result of the function must be a message that specifies the amount of characters that still remain for the user to reach the minimum necessary to send the message. When the counter is at zero the message says 100 characters are missing.
The problem here is that I don’t know why when I empty the text field it doesn’t update and then count 1 as zero and 2 as 1 and 101 as 100.
I need that when there are no characters typed in the input the message is the first if where the message says the minimum of characters for the message to be sent is 100.
const handleTextCharactersCount = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
setCharactersCount(event.target.value.length)
if (charactersCount === 0) {
setHelperMessage('Descreva o conteúdo do vídeo. Mínimo 100 Caracteres')
}
if (charactersCount > 0) {
setHelperMessage(
`Falatam ${
100 - charactersCount
} para que você possa enviar a mensagem`,
)
}
if (charactersCount > 100) {
setHelperMessage('Mínimo de caracateres para envio atingido.')
}
},
[charactersCount],
)
Then the character counter would not even be needed but a constant within the callback itself. Thanks for the help.
– Antonio Carlos Araújo