How to concatenate ' /n ' with useState()?

Hello

I need to retrieve all the content stored in useState and concatenate a line break to useState, whenever ENTER is pressed. What the code is displaying in the HTML when retrieving the useState values { useText } is a SPACE.

My Code:

const [useText, setUseText] = useState();

const setText = ( event ) => {
( event.code === ‘Enter’ )
? setUseText( useText.replace(/\n/g, ‘\n’) )
: setUseText( event.value );
}

Thanks

( event.code === ‘Enter’ )
You should replace the backticks with single quotes.
event.value should be replaced with event.target.value .
In React, when handling input events, you need to access the value property of the target property of the event object.

‘\n’)
replace the backticks with single quotes.

const [useText, setUseText] = useState(‘’);
This means that the useText state variable will be initially set to an empty string, and you can use it immediately with this initial value.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.