I want to have an input whose width adapts to fit its content.
I'm trying to implement this answer to a similar question, but using React:
import React, { useState } from 'react';
export default () => {
const [content, setContent] = useState('');
const [width, setWidth] = useState(0);
const changeHandler = evt => {
setContent(evt.target.value);
};
return (
<wrapper>
<span id="hide">{content}</span>
<input type="text" autoFocus style={{ width }} onChange={changeHandler} />
</wrapper>
);
};
The problem is I don't know how to then query the width of the span, in order to then change the width of the input (using setWidth
).
How can I achieve this?