const [text, setText] = useState('Hello friend')
return
<p>{text}</p>
How can I put a cursor between a hello and a friend and write something? It should look like I write in a p or div tag, not like I write in a text area or input.
const [text, setText] = useState('Hello friend')
return
<p>{text}</p>
How can I put a cursor between a hello and a friend and write something? It should look like I write in a p or div tag, not like I write in a text area or input.
Use contentEditable
attribute, and useRef
:
import React, { useRef } from "react";
export default function App() {
const text = useRef("Hello friend");
const handleChange = e => {
text.current = e.target.innerText
};
return (
<p contentEditable="true" onInput={handleChange}>
{text.current}
</p>
);
}
Making this work with useState
might be difficult. Could you use an <input>
and style it to look like a <p>
?
input {
border: 0;
background: transparent;
}