javascript - 以编程方式反应更新参考
问题描述
我有一个输入结构和一个增长的容器(自动扩展文本区域)。输入容器处于绝对位置,我需要始终知道该容器的 offsetHeight。为此,我使用useRef
.
所以结构看起来像
const [text, setText] = useState('')
const handleTextChange = (e) => setText(e.target.value)
const ref = useRef(null)
const offsetHeight = ref.current && ref.current.offsetHeight || 70
return (
<div className="root">
<SomethingFancy offsetHeight={offsetHeight}>{offsetHeight}
</SomethingFancy>
<div className="input-container-absolute-position" ref={ref}
style={{ position: 'absolute', bottom: 0 }}>
<textarea onChange={handleTextChange} value={text} />
</div>
</div>
什么有效:
- 如果我输入任何文本并在输入的高度上添加新行,则 .offsetHeight 会更新,工作正常。
什么坏了:
- 如果我删除文本:我有 2 行文本
offsetHeight=100px
,删除文本使其变为 1 行 (70px
),但更改不适用于<SomethingFancy />
.
解决方案
推荐阅读
- html - Node.js 应用程序的结构
- python - TDA api检查账户余额
- unix - 从目录中复制文件中包含特定句子的前 100 个文件
- c++ - Boost Process 1.71 不再查看环境路径
- javascript - TypeError: undefined is not a function ('...this.state.profile.map...'附近)
- math - 将平面从点/法线/D 转换为平面方程
- react-native - flowtype 插件影响代码的其他部分
- sql - 删除字符串 SQL 中的电子邮件地址
- flutter - 如何在颤动中限制 GridView 小部件的宽度
- sas - SAS - 如何配置 sas 以使用本地磁盘而非本地磁盘 C 中的资源: